Reporting promise rejections

In some apps, it may be useful to report unhandled Promise rejections in the same way you would report unhandled Errors.

Bugsnag makes it easy to automatically report unhandled rejections in browsers that support the unhandledrejection event. See MDN for more information on browser support.

How to enable

To reduce confusion due to poor browser support of the unhandledrejection event, this feature is off by default. It can be turned on and off with:

// Enable notifying on unhandledrejection in browsers that support it:
Bugsnag.enableNotifyUnhandledRejections();

// Disable notifying on unhandledrejection (off by default):
Bugsnag.disableNotifyUnhandledRejections();

Third-party Promise support

If you are using a third-party Promise library, it may not trigger any onunhandledrejection handlers. Additional work may be required to notify Bugsnag about unhandled Promise rejections in your library.

Below are some examples of popular third-party Promise libraries and their built-in hooks for catching rejections. If you’re using a library not listed below, it’s worth checking their docs to see if they provide similar functionality.

Bluebird

Bluebird fires the unhandledrejection event for unhandled rejections, so enabling the notifyUnhandledRejections option as described above should be all you need to automatically report unhandled rejections to Bugsnag.

RSVP.js

RSVP exposes an error event, which gives you a simple way to notify unhandled rejections.

RSVP.on('error', function(reason) {
  Bugsnag.notifyException(reason);
});

There is no need to enable the notifyUnhandledRejections option to report RSVP errors to Bugsnag this way.

Angular 1.x

If you’re using angular in your app, the angular-bugsnag library uses the $exceptionHandler service to catch any unhandled $q rejections. More about unhandled rejections in angular:

jQuery

jQuery uses a Promise-like object called a Deferred to implement many async behaviors.

Currently there isn’t a public, well-documented interface to globally report unhandled Deferred rejections, but by setting the internal deferred exception hook to your own handler, you can report Deferred errors to a service like Bugsnag.

Note that the exceptionHook will be called even if something later in the chain may catch the Deferred error, so you will likely want to add some logic around the call to Bugsnag.notifyException to filter out errors you may not care about.

jQuery.Deferred.exceptionHook = function( error, stack ) {
  var shouldReport = false;

  // Add logic here to determine which errors should be reported to Bugsnag
  //    shouldReport = ???

  if (shouldReport) {
    Bugsnag.notifyException(error);
  }
};

There is no need to enable the notifyUnhandledRejections option to report jQuery deferred errors to Bugsnag this way.

Other

You can manually add Bugsnag.notifyException to your Promise chains to catch rejections, like so;

doSomethingDangerous()
  .then(andThenSomethingElse)
  .catch(Bugsnag.notifyException);

If you need a solution which works more automatically, you could use a library like unhandled-rejection:

const rejectionEmitter = unhandledRejection({
    timeout: 20
});

rejectionEmitter.on("unhandledRejection", (error, promise) => {
    Bugsnag.notifyException(error);
});

Neither of these methods require you to enable the Bugsnag notifyUnhandledRejections option to report to Bugsnag.