question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Built in Custom Commands are invoked with wrong parameters if callback throws exception

See original GitHub issue

Version 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 logged
  • before exception null false is logged
  • Uncaught ReferenceError: throwExceptionWithNonExistingFunction is not defined error is logged

Considerations:

  1. 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 right tcData and success === true, then with null tcData and success === 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 to try...catch the callback as the __tcfapi behaviour could not be reliable in case of exceptions
  2. 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 only tcData and success 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))
  3. 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
matitacommented, Oct 17, 2020

@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:

  protected invokeCallback(response: any): void {

	const success = response !== null;

    if (typeof this.next === 'function') {

      this.callback(this.next, response, success);

    } else {

      this.callback(response, success);

    }

  }
1reaction
shortaflipcommented, Apr 27, 2021

v1.2.1 of the library that contains the changes from the PR that closed this ticket is live.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found