civinomics / city-council

MIT License
1 stars 2 forks source link

Setup Airbrake for your JavaScript application #150

Closed drew-moore closed 7 years ago

drew-moore commented 7 years ago

Installation

Using npm

npm install airbrake-js

Using bower

bower install airbrake-js-client

Setup

The notifier is built using a standalone browserify build and can be used with:

We include the full source code with the package, so you can use Browserify too.

If you prefer not to host the library yourself, airbrake-js is available on the excellent cdnjs CDN.

Basic Usage

First you need to initialize the notifier (You can find your project ID and API KEY with your project's settings):


var airbrake = new airbrakeJs.Client({projectId: <Your project ID>, projectKey: '<Your project API KEY>'});

Or if you are using browserify/webpack/etc:

var airbrakeJs = require('airbrake-js');

var airbrake = new airbrakeJs({projectId: <Your project ID>, projectKey: '<Your project API KEY>'});

The simplest method is to report 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();

For more information please visit our official GitHub Repo.