Reporting Errors
Honeybadger reports uncaught exceptions automatically. In all
other cases, use Honeybadger.notify(error)
to send any error to
Honeybadger.
To catch an exception and notify Honeybadger without re-throwing:
try {
throw('oops');
} catch(error) {
Honeybadger.notify(error)
}
Customizing the Error Name
JavaScript often uses generic class names -- such as Error
-- which are
uninformative and also cause unrelated errors to be grouped together. To get
around this issue it's a good practice to send a custom error class when
notifying Honeybadger:
Honeybadger.notify(error, 'DescriptiveClass');
Additional Options
You can also set or override other optional data which is reported with the error:
Honeybadger.notify(error, {
message: 'My custom message',
name: 'DescriptiveClass',
component: 'badgers',
action: 'show',
context: { badgerId: 1 },
fingerprint: 'This unique string will group similar errors together',
environment: 'production',
projectRoot: 'https://www.example.com/',
params: { key: 'value' },
cookies: { key: 'value' } // May also be sent as a string in the document.cookie "foo=bar;bar=baz" format.
});
Other Ways to Notify
You can notify Honeybadger of anything, even if you don't have an error object:
Honeybadger.notify('Badgers!');
Honeybadger.notify('Badgers!', { ... });
Honeybadger.notify('Badgers!', 'CustomClass');
Honeybadger.notify('Badgers!', 'CustomClass', { ... });
Honeybadger.notify({
message: 'Badgers!',
name: 'CustomClass',
...
});
A stacktrace will be generated for you (when possible) if you do not provide an error object.
Wrapping Callbacks (or whatever)
If you happen to use the following pattern a lot, for instance, to wrap asynchonous callbacks:
try {
// ...error producing code...
} catch(error) {
Honeybadger.notify(error)
throw(error);
}
A nicer option is to use Honeybadger.wrap
. You pass it a function. It returns
a new function which wraps your existing function in a try/catch block which
will notify Honeybadger and re-throw:
Honeybadger.wrap(function(){
throw "oops";
})();
Note that wrap
returns a function. This makes it easy to use with event
handlers, as in the example below:
$(document).on("click", "#myElement", Honeybadger.wrap(function(){ throw "oops"; }));