bugsnag-js icon indicating copy to clipboard operation
bugsnag-js copied to clipboard

OnErrorCallback must return Promise<void | boolean>

Open Nigui opened this issue 4 years ago • 4 comments

Describe the bug

From VuePlugin, I add an onError callback to add global metadata on each error event. I'm using the async/await syntax in onError callback.

I found that if I don't return a value ( I'm returning true), event is not sent and following error is printed

'Event not sent due to onError callback'

If my code read is good, explanation is error comes from that line and is caused by that other line

Fix could be to make that type return Promise<void|boolean>

Environment

  • Bugsnag version: 7.2.0
  • Browser framework version (if any):
    • Vue: 2.6.11
  • Browser version (e.g. chrome, safari): n/a
  • Device (e.g. iphonex): n/a

Example code snippet

// init bugsnag plugin with config then

// doesn't work
Bugsnag.addOnError(async (event) => {
    event.addMetadata("blah", await computeMetadata() );
})

// works
Bugsnag.addOnError(async(event) => {
    event.addMetadata("blah", await computeMetadata());
    return true;
})

Error messages:
 'Event not sent due to onError callback'

Nigui avatar Nov 25 '20 17:11 Nigui

Hi @Nigui,

Would you be able to share the rest of your Bugsnag configuration, particularly the Bugsang.start() code you are using and also the structure of your computeMetadata() method.

Feel free to write in to [email protected] if you don't want to share that code here. Thanks

johnkiely1 avatar Nov 26 '20 11:11 johnkiely1

Of course ! Here it is:


   import Bugsnag, { Event, OnErrorCallback } from "@bugsnag/js";

   const config: BrowserConfig = {
       apiKey: process.env.BUGSNAG_API_KEY!,
       appVersion: process.env.APP_COMMIT,
       releaseStage: process.env.ENV,
       plugins: [new BugsnagPluginVue(Vue)],
   };

   Bugsnag.start(config);

   const addMetadataOnError = async (event: Event) => {
       event.addMetadata("metadata", {
           key: await computeAsyncValue(),
       });
       return true // doesn't work without
   }


   Bugsnag.addOnError((addMetadataOnError as unknown) as OnErrorCallback); // must force type because OnErrorCallback does not accept return Promise<boolean>

Nigui avatar Nov 27 '20 10:11 Nigui

Thanks @Nigui ,

We will look into getting this corrected.

johnkiely1 avatar Dec 08 '20 16:12 johnkiely1

Hey @Nigui,

Thanks for the report; I've fixed the OnErrorCallback return type in https://github.com/bugsnag/bugsnag-js/pull/1224 so that it allows Promise<boolean>

I haven't been able to reproduce this problem though:

I found that if I don't return a value ( I'm returning true), event is not sent and following error is printed

'Event not sent due to onError callback'

Your example callback works for me with or without the return true, after ignoring the Typescript error. I'm not sure if I'm misunderstanding though; was your only issue the Typescript error?

imjoehaines avatar Jan 06 '21 15:01 imjoehaines