This is the JavaScript notifier for capturing errors in web browsers and reporting them to Airbrake.
Using npm:
npm install airbrake-js
or using Bower:
bower install airbrake-js-client
Example configurations can be found in examples, including:
- Angular
- Angular 2
- Bower
- Browserify
- Express.js
- hapi.js
- Legacy
- Node.js
- Rails
- React
- Redux
- RequireJS
- Vue.js
The notifier is built using umd and therefore can be imported with AMD, CommonJS2 or as property in root.
If you prefer not to host the library yourself, airbrake-js is available on the excellent cdnjs.
If you're using Node.js you might need to install the request library too, on which Airbrake depends:
npm install request
First you need to initialize the notifier with the project id and API key taken from Airbrake.io:
var airbrake = new airbrakeJs.Client({projectId: 1, projectKey: 'abc'});
Or if you are using browserify/webpack/etc:
var AirbrakeClient = require('airbrake-js');
var airbrake = new AirbrakeClient({projectId: 1, projectKey: 'abc'});
Then you can send a textual message to Airbrake:
var promise = airbrake.notify(`user id=${user_id} not found`);
promise.then(function(notice) {
console.log('notice id', notice.id);
});
promise.catch(function(err) {
console.log('airbrake error', err);
});
Or report catched errors directly:
try {
// This will throw if the document has no head tag
document.head.insertBefore(document.createElement('style'));
} catch(err) {
airbrake.notify(err);
throw err;
}
Alternatively, you can wrap any code which may throw errors using the client's wrap
method:
var startApp = function() {
// This will throw if the document has no head tag.
document.head.insertBefore(document.createElement('style'));
}
startApp = airbrake.wrap(startApp);
// Any exceptions thrown in startApp will be reported to Airbrake.
startApp();
or use call
shortcut:
var startApp = function() {
// This will throw if the document has no head tag.
document.head.insertBefore(document.createElement('style'));
}
airbrake.call(startApp);
It's possible to annotate error notices with all sorts of useful information at the time they're captured by supplying it in the object being reported.
try {
startApp();
} catch (err) {
airbrake.notify({
error: err,
context: { component: 'bootstrap' },
environment: { env1: 'value' },
params: { param1: 'value' },
session: { session1: 'value' },
});
throw err;
}
Severity allows categorizing how severe an error is. By default, it's set to error
. To redefine severity, simply overwrite context/severity
of a notice object. For example:
airbrake.notify({
error: err,
context: { severity: 'warning' }
});
There may be some errors thrown in your application that you're not interested in sending to Airbrake, such as errors thrown by 3rd-party libraries, or by browser extensions run by your users.
The Airbrake notifier makes it simple to ignore this chaff while still processing legitimate errors. Add filters to the notifier by providing filter functions to addFilter
.
addFilter
accepts the entire error notice to be sent to Airbrake, and provides access to the context
, environment
, params
, and session
values submitted with the notice, as well as the single-element errors
array with its backtrace
element and associated backtrace lines.
The return value of the filter function determines whether or not the error notice will be submitted.
- If a null value is returned, the notice is ignored.
- Otherwise, the returned notice will be submitted.
An error notice must pass all provided filters to be submitted.
In the following example all errors triggered by admins will be ignored:
airbrake.addFilter(function(notice) {
if (notice.sessions.admin) {
// Ignore errors from admin sessions.
return null;
}
return notice;
});
Filters can be also used to modify notice payload, e.g. to set the environment and application version:
airbrake.addFilter(function(notice) {
notice.context.environment = 'production';
notice.context.version = '1.2.3';
return notice;
});
In order to enable source map support you have to specify the path to the source map file according to the source map specification. For example, airbrake.min.js has the following line:
//# sourceMappingURL=airbrake.min.map
Please note that the Airbrake backend downloads the source map file to process the backtrace. This means that the source map should be publicly accessible via HTTP. So, for example, don't expect source map support to work on your local webserver running on localhost
.
Custom source map URLs are supported by assigning a special property of notice.context
called sourceMaps
. The keys of the sourceMaps
object represent shell filename patterns and the values are URLs of your source maps.
airbrake.addFilter(function(notice) {
notice.context.sourceMaps = {
'*': 'https://domain.com/path/to/source.map', // for all files
'https://domain.com/path/to/file.min.js': 'https://domain.com/path/to/source.map'
};
return notice;
});
If you're interested in inspecting the information reported to Airbrake in your own code, you can register your own error reporter. Note that reporters added this way may be executed out-of-order.
In this example, reported errors are also logged to the console.
<script>
airbrake.addReporter(function(notice) {
console.log(notice);
});
</script>
airbrake-js automatically wraps console.log
function calls in order to collect logs and send them with first error. You can undo it using following code:
if (env === 'development') {
let methods = ['debug', 'log', 'info', 'warn', 'error'];
for (let m of methods) {
if (m in console && console[m].inner) {
console[m] = console[m].inner;
}
}
}
airbrake-js automatically setups window.onerror
handler when script is loaded. It also makes sure to call old error handler if there are any. Errors reported by window.onerror
can be ignored using ignoreWindowError
option:
var airbrake = new airbrakeJs.Client({ignoreWindowError: true});
See https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onerror#Notes.
Install dependencies:
npm install
Run unit tests:
karma start
Build project:
webpack
Airbrake is maintained and funded by airbrake.io
Thank you to all the contributors.
The names and logos for Airbrake are trademarks of Airbrake Technologies Inc.
Airbrake is Copyright © 2008-2017 Airbrake Technologies Inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.