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.

These docs are for the @bugsnag/react-native package, which supports React Native v0.60 and above.

If you’re using the previous bugsnag-react-native package, we recommend upgrading to the latest release, unless you’re using a version of React Native older than 0.60. Documentation for bugsnag-react-native can be found on our legacy pages.

Basic usage

If you would like to send handled errors to BugSnag, you can pass an error to the notify method in any of the three libraries:

try {
    // Some potentially crashy code
} catch (e) {
    Bugsnag.notify(e);
}
try {
    // Some potentially crashy code
} catch (Throwable e) {
    Bugsnag.notify(e);
}
@try {
    [NSJSONSerialization dataWithJSONObject:badlyFormattedJson options:0 error:nil];
} @catch (NSException* exception) {
    [Bugsnag notify:exception];
}

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

Customizing diagnostic data

You can send handled exceptions with diagnostic data or other customizations by passing a callback function/block as an argument to notify (and/or notifyError on iOS).

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@bugsnag.com', 'Bugs Nag')
  event.addMetadata('hyperflopz', { count: 23 })
})
try {
    // Some potentially crashy code
} catch (Throwable exception) {
    Bugsnag.notify(exception, new OnErrorCallback() {
        @Override
        public boolean onError(Event event) {
            // Add extra information
            event.addMetadata("account", "name", "Acme Co.")
            event.addMetadata("account", "paying_customer", true)

            // Increase severity
            event.setSeverity(Severity.ERROR)

            // Return `false` if you'd like to stop this error being reported
            return true;
        }
    });
}
[Bugsnag notifyError:error block:^BOOL(BugsnagEvent *event) {
    // Add extra information
    [event addMetadata:@"Acme Co." withKey:@"name" toSection:@"account"];
    [event addMetadata:@(YES) withKey:@"paying_customer" toSection:@"account"];

    // Increase severity
    event.severity = BSGSeverityError;

    // Return `NO` if you'd like to stop this error being reported
    return YES;
}];

For more information on using the notify method, see our platform-specific guides:

The Event object

The notify callback provides an “event” parameter that represents an error captured by BugSnag. The object contains properties and methods for you to query and update the captured data.

For full details, see our platform-specific guides: