npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

airbrake-js-chimpy

v1.5.0

Published

Notify Airbrake on JavaScript exceptions

Downloads

3

Readme

Airbrake-JS

Build Status CDNJS

This is the JavaScript notifier for capturing errors in web browsers and reporting them to Airbrake.

Installation

Using npm:

npm install airbrake-js

or using Bower:

bower install airbrake-js-client

Setup

Example configurations can be found in examples, including:

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 Webpack in browser context you might need to mark certain Node.js packages as external dependencies to decrease bundle size.

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:

Unwrapping console

airbrake-js automatically wraps console.log function calls in order to collect logs and send them with first error. You can undo it using unwrapConsole option:

var airbrake = new airbrakeJs.Client({
  ...
  unwrapConsole: true,
});

Node.js request and proxy

In order to configure request HTTP client you can pass request option which accepts request wrapper:

var airbrake = new AirbrakeClient({
  ...
  request: request.defaults({'proxy':'http://localproxy.com'})
});

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

What does "Script error" mean?

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

Contributing

Install dependencies:

npm install

Run unit tests:

karma start

Build project:

webpack

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.