1.6.8 • Published 5 years ago

airbrake-js v1.6.8

Weekly downloads
20,751
License
MIT
Repository
github
Last release
5 years ago

Airbrake for web browsers

Build Status CDNJS

This is the JavaScript notifier for capturing errors in web browsers and reporting them to Airbrake. For Node.js there is a separate package.

Installation

airbrake-js can be installed using yarn:

yarn add airbrake-js

or using npm:

npm install airbrake-js

Setup

Starting from v2 airbrake-js uses rollup.js to provide 3 separate build formats:

  • dist/airbrake.iife.js - a self-executing function, suitable for inclusion as a tag.
  • dist/airbrake.esm.js - an ES module file, suitable for other bundlers and inclusion as a tag in modern browsers.
  • dist/airbrake.common.js - CommonJS, suitable for Node.js and other bundlers.

Your package manager should automatically pick suitable bundle format based on airbrake-js package.json file:

  "main": "dist/airbrake.common.js",
  "web": "dist/airbrake.iife.js",
  "module": "dist/airbrake.esm.js",
  "jsnext:main": "dist/airbrake.esm.js",
  "types": "dist/airbrake.d.ts",
  "source": "src/index.ts",

Example configurations can be found in examples, including:

Basic Usage

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: 'REPLACE_ME',
  environment: 'production',
});

Or if you are using browserify/webpack/etc:

var AirbrakeClient = require('airbrake-js');
var airbrake = new AirbrakeClient({...});

Then you can send a textual message to Airbrake:

var promise = airbrake.notify(`user id=${user_id} not found`);
promise.then(function(notice) {
  if (notice.id) {
    console.log('notice id', notice.id);
  } else {
    console.log('notify failed', notice.error);
  }
});

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);

Advanced Usage

Notice Annotations

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

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' }
});

Filtering errors

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.params.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;
});

Filtering keys

With keysBlacklist option you can specify list of keys containing sensitive information that must be filtered out, e.g.:

var airbrake = new AirbrakeClient({
    ...
    keysBlacklist: [
      'password', // exact match
      /secret/, // regexp match
    ],
});

Source maps

Airbrake supports using private and public source maps. Check out our docs for more info:

Instrumentation

airbrake-js automatically instruments console.log function calls in order to collect logs and send them with first error. You can disable that behavior using instrumentation option:

var airbrake = new airbrakeJs.Client({
  ...
  instrumentation: {
    console: false,
  },
});

Integration

window.onerror

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});

FAQ

What does "Script error" mean?

See https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onerror#Notes.

Contributing

Install dependencies:

yarn install

Run unit tests:

yarn test

Build project:

yarn build

Credits

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.

License

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.

2.0.0-beta.3

5 years ago

2.0.0-beta.2

5 years ago

2.0.0-beta

5 years ago

1.6.8

5 years ago

1.6.7

5 years ago

1.6.6

5 years ago

1.6.5

5 years ago

1.6.4

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.6.0-beta.2

5 years ago

1.6.0-beta.1

5 years ago

1.6.0-beta

5 years ago

1.5.0

6 years ago

1.5.0-beta

6 years ago

1.4.9

6 years ago

1.4.8

6 years ago

1.4.7

6 years ago

1.4.6

6 years ago

1.4.5

6 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.9.9

6 years ago

0.9.8

7 years ago

0.9.7

7 years ago

0.9.6

7 years ago

0.9.5

7 years ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.8

7 years ago

0.8.7

7 years ago

0.8.6

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.0

7 years ago

0.8.0-rc.1

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.7.0-rc.11

7 years ago

0.7.0-rc.10

7 years ago

0.7.0-rc.9

7 years ago

0.7.0-rc.8

7 years ago

0.7.0-rc.7

7 years ago

0.7.0-rc.6

7 years ago

0.7.0-rc.5

7 years ago

0.7.0-rc.4

7 years ago

0.7.0-rc.3

7 years ago

0.7.0-rc.2

7 years ago

0.7.0-rc.1

7 years ago

0.6.0

7 years ago

0.6.0-beta.9

7 years ago

0.6.0-beta.8

7 years ago

0.6.0-beta.7

7 years ago

0.6.0-beta.6

7 years ago

0.6.0-beta.5

7 years ago

0.6.0-beta.4

7 years ago

0.6.0-beta.3

7 years ago

0.6.0-beta.2

7 years ago

0.6.0-beta

7 years ago

0.6.0-alpha.3

7 years ago

0.6.0-alpha.2

7 years ago

0.6.0-alpha

7 years ago

0.5.9

8 years ago

0.5.8

8 years ago

0.5.7

8 years ago

0.5.6

8 years ago

0.5.4

8 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.5.0-alpha

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.0

9 years ago

0.4.0-alpha.2

9 years ago

0.4.0-alpha.1

9 years ago

0.4.0-alpha.0

9 years ago

0.3.12

9 years ago

0.3.11

9 years ago

0.3.9

9 years ago