Built in Custom Commands are invoked with wrong parameters if callback throws exception
See original GitHub issueVersion
1.1.0-6
Module (core, cmpapi, cli, stub, or testing)
cmpapi
Describe with reproduction steps – What is the expected behavior? Code to reproduce the issue:
import { CmpApi } from '@iabtcf/cmpapi';
const api = new CmpApi(123, 456, true, {
addEventListener: (next, tcData, success) => {
if (tcData) {
tcData.someProp = 'some value';
}
if (typeof next === 'function') {
next(tcData, success);
} else {
console.error('next is not a function');
}
},
});
api.update('', false);
window.__tcfapi('addEventListener', 2, (tcData, success) => {
console.log('before exception', tcData, success);
throwExceptionWithNonExistingFunction();
console.log('after exception', tcData, success);
});
Current behaviour:
next is not a function
is logged
Expected behaviour (which is what happens if no built-in custom command is specified):
before exception {...} true
is loggedbefore exception null false
is loggedUncaught ReferenceError: throwExceptionWithNonExistingFunction is not defined
error is logged
Considerations:
- TCF API spec does not define what should be the behaviour in case the
__tcfapi
callback throws exception, currently the callback is invoked twice: first with righttcData
andsuccess === true
, then with nulltcData
andsuccess === false
. It makes sense but the side effect is that the callback is invoked twice, maybe there should be some official spec on how__tcfapi
should behave in case of callback’s exception, or at least specify that it’s up to the consumer totry...catch
the callback as the__tcfapi
behaviour could not be reliable in case of exceptions - I think built-in custom commands should always be invoked with
next
as a function, we have another similar case where they are invoked with onlytcData
andsuccess
as parameters, which I guess this PR should solve: https://github.com/InteractiveAdvertisingBureau/iabtcf-es/pull/212 (e.g.__tcfapi('getTCData', 2, function(tcData, success) {}, null)
) - this should be the code that exposes the issue: https://github.com/InteractiveAdvertisingBureau/iabtcf-es/blob/b4152095ebc3b494d5e69aebf32c608b6a6ecf4d/modules/cmpapi/src/command/Command.ts#L22-L30
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Message "Async callback was not invoked within the 5000 ms ...
I'm using Puppeteer and Jest to run some front end tests. My tests look as follows: describe("Profile Tab Exists and Clickable: /settings/user", ()...
Read more >Using .then(), .catch(), .finally() to Handle Errors in Javascript ...
code to run if there are any problems }. When a problem arises and is then handled, an “exception is thrown” by the...
Read more >Error Handling in Express - Reflectoring
Handling errors thrown by asynchronous functions invoked in the routes defined in the Express ... Default Built-in Error Handler of Express.
Read more >Node.js Error Handling Best Practices - Sematext
Learn what is Node.js error handling and why do you need it. From using middleware to catching uncaught exceptions, discover the best ways ......
Read more >Better Error Handling In NodeJS With Error Classes
You can literally throw any data type this openness is not allowed by other ... Bad Error Handling Pattern #1: Wrong Use Of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@chrispaterson I might be wrong, but I think it would just be a matter of changing this code: https://github.com/InteractiveAdvertisingBureau/iabtcf-es/blob/b4152095ebc3b494d5e69aebf32c608b6a6ecf4d/modules/cmpapi/src/command/Command.ts#L35-L55
into this code:
v1.2.1 of the library that contains the changes from the PR that closed this ticket is live.