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.
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”.
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 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: