Reporting handled errors

In order to quickly understand and fix errors, it is often helpful to send exceptions that have been handled by your application to Bugsnag, with additional diagnostic data that is specific to the error.

Basic usage

If you would like to send handled errors to Bugsnag, you can pass an error to the Bugsnag.notify method.

try {
    // Some potentially crashy code
} catch (e) {

This will create and send a report for a “handled” error, with the default severity “warning”.

We recommend passing a JavaScript Error object to notify as this is ensures that an accurate stacktrace can be obtained.

If you provide something that is not an Error, such as a string or an object with name and message fields, notify will attempt to generate a stacktrace. This works for most cases, but is not as reliable as when an error is provided. If notify is unable to use the object passed in, for example null or a Function, the error will be reported to your dashboard but the message will indicate a notifier usage error.

Handled promise rejections

Similarly, notify can be used to send promise rejections as a handled error to Bugsnag:

new Promise(function(resolve, reject) {
.then(function () { /* if the promise is resolved */ })
.catch(Bugsnag.notify); // if the promise is rejected

Customizing diagnostic data

You can send handled exceptions with diagnostic data or other customizations by passing an onError callback as an argument to notify.

The callback receives an Event object as a parameter which can be used to add or amend the data sent to your Bugsnag dashboard. You can also return false from the callback to prevent the event being sent at all:

Bugsnag.notify(new Error('Bad, but not fatal'), function (event) {
  if (event.getUser().id === '1') return false
  event.severity = 'info'
  event.context = 'component-space-246'
  event.setUser('3', '', 'Bugs Nag')
  event.addMetadata('hyperflopz', { count: 23 })

See Customizing error reports for more information about the properties and methods available in an Event object.

Post-report callbacks

To run a callback when the report has been delivered or enqueued for delivery later, provide this as the third argument:

Bugsnag.notify(new Error('uh oh'), null, function (err, event) {
  if (err) {
    console.log('Failed to send report because of:\n' + err.stack)
  } else {
    console.log('Successfully sent report "' + event.errors[0].errorMessage + '"')