パッケージの詳細

@airbrake/node

airbrake68.5kMIT2.1.9

Official Airbrake notifier for Node.js

exception, error, airbrake, notifier

readme

Official Airbrake Notifier for Node.js

Build Status npm version npm dm npm dt

The official Airbrake notifier for capturing JavaScript errors in Node.js and reporting them to Airbrake. If you're looking for browser support, there is a separate package.

Installation

Using yarn:

yarn add @airbrake/node

Using npm:

npm install @airbrake/node

Basic Usage

First, initialize the notifier with the project ID and project key taken from Airbrake. To find your project_id and project_key navigate to your project's Settings and copy the values from the right sidebar:

const { Notifier } = require('@airbrake/node');

const airbrake = new Notifier({
  projectId: 1,
  projectKey: 'REPLACE_ME',
  environment: 'production',
});

Then, you can send a textual message to Airbrake:

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

or report errors directly:

try {
  throw new Error('Hello from Airbrake!');
} catch (err) {
  airbrake.notify(err);
}

Alternatively, you can wrap any code which may throw errors using the wrap method:

let startApp = () => {
  throw new Error('Hello from Airbrake!');
};
startApp = airbrake.wrap(startApp);

// Any exceptions thrown in startApp will be reported to Airbrake.
startApp();

or use the call shortcut:

let startApp = () => {
  throw new Error('Hello from Airbrake!');
};

airbrake.call(startApp);

Example configurations

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

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:

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.

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 properties. It also includes the single-element errors array with its backtrace property and associated backtrace lines.

The return value of the filter function determines whether or not the error notice will be submitted.

  • If null 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((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((notice) => {
  notice.context.environment = 'production';
  notice.context.version = '1.2.3';
  return notice;
});

Filtering keys

With the keysBlocklist option, you can specify a list of keys containing sensitive information that must be filtered out:

const airbrake = new Notifier({
  // ...
  keysBlocklist: [
    'password', // exact match
    /secret/, // regexp match
  ],
});

Node.js request and proxy

To use the request HTTP client, pass the request option which accepts a request wrapper:

const airbrake = new Notifier({
  // ...
  request: request.defaults({ proxy: 'http://localproxy.com' }),
});

Instrumentation

@airbrake/node attempts to automatically instrument various performance metrics. You can disable that behavior using the performanceStats option:

const airbrake = new Notifier({
  // ...
  performanceStats: false,
});

Filtering performance data

addPerformanceFilter allows for filtering performance data. Return null in the filter to prevent that metric from being reported to Airbrake.

airbrake.addPerformanceFilter((metric) => {
  if (metric.route === '/foo') {
    // Requests to '/foo' will not be reported
    return null;
  }
  return metric;
});

更新履歴

Airbrake JS Changelog

master

[2.1.9] (March 6, 2025)

browser

  • Added the keysAllowlist option, which is a counter-part to the keysBlocklist option. It filters out all the data from the notice except the specified keys (#1335)
  • Added support for error reporting of "falsey" errors such as null, NaN, undefined, false, "" (#1345)
  • Added the instrumentation.unhandledrejection option, which enables/disables the Airbrake handler for the unhandledrejection event (#1356)

2.1.8 (December 6, 2022)

browser

  • Fixed relative import issues with Yarn's Plug'n'Play feature (#1135)
  • Stop filtering the context field in the notice payload. This payload contains service information and it should never be modified (#1325)
  • Bumped cross-fetch dependency to ^3.1.5 (fixes a Dependabot security alert) (#1322)

2.1.7 (October 4, 2021)

  • [browser/node] Fixed incorrect yarn.lock references (#1132)

2.1.6 (October 4, 2021)

  • [browser] Fixed not being able to attach a response type when sending a performance breakdown (#1128)

2.1.5 (June 2, 2021)

  • [node] Specify which versions of node are supported (#1038)

2.1.4 (April 16, 2021)

  • [browser] Fixed TypeError: undefined is not an object (evaluating 'e.searchParams.append') occurring in old browsers that don't support Object.entries (such as Internet Explorer) (#1001, #1002)

2.1.3 (February 22, 2021)

  • [browser/node] Fixed missing library files in v2.1.2

2.1.2 (February 22, 2021)

  • [browser] Started catching errors in promises that occur in RemoteSettings (#949)

2.1.1 (February 20, 2021)

  • [browser] Removed unwanted debugger statement in base_notifier.js in the distribution package (#948)

2.1.0 (February 19, 2021)

  • [browser/node] Added the queryStats and the queueStats option. They allow/forbid reporting of queries or queues, respectively (#945)
  • [browser/node] Fixed _ignoreNextWindowError undefined error when wrapping errors (#944)
  • [node] Fixed warnings on loading of notifier.js when using Webpack (#936)

2.0.0 (February 18, 2021)

  • [browser/node] Removed deprecated ignoreWindowError option (#929)
  • [browser/node] Removed deprecated keysBlacklist option (#930)
  • [browser/node] Introduced the remoteConfigHost option. This option configures the host that the notifier fetch remote configuration from. (#940)
  • [browser/node] Introduced the apmHost option. This option configures the host that the notifier should send APM events to. (#940)
  • [browser/node] Introduced the errorNotifications option. This options configures ability to send errors (#940)
  • [browser/node] Introduced the remoteConfig option. This option configures the remote configuration feature (#940)
  • [browser/node] Added support for the remote configuration feature (#940)

1.4.2 (December 22, 2020)

Changed

  • [node] Conditionally initialize ScopeManager (#894)
  • [browser] Add the ability to disable console tracking via instrumentation (#860)

1.4.1 (August 10, 2020)

Changed

  • [browser] Unhandled rejection errors now include unhandledRejection: true as part of their context (#795)

1.4.0 (July 22, 2020)

Changed

  • [browser/node] notify now includes the url property on the returned INotice object (#780)

1.3.0 (June 19, 2020)

Changed

  • [browser/node] Deprecate keysBlacklist in favor of keysBlocklist

1.2.0 (May 29, 2020)

Added

  • [node] New method to filter performance metrics (#726)

1.1.3 (May 26, 2020)

Changed

  • [browser/node] Remove onUnhandledrejection parameter type

1.1.2 (May 5, 2020)

Fixed

  • [browser] Add guard for window being undefined (#684)
  • [node] Report URL using req.originalUrl instead of req.path in Express apps (#691)

1.1.1 (April 28, 2020)

Fixed

  • [node] Express route stat reporting (#671)

1.1.0 (April 22, 2020)

Changed

  • [browser/node] Build process updates. Bumping minor version for this. See #646
  • [browser/node] Documentation updates

1.0.7 (April 8, 2020)

Added

  • [node] New config option to disable performance stats

Changed

  • [browser/node] Build config updates
  • [browser/node] Update dependencies
  • [browser/node] Documentation updates
  • [browser/node] Update linting config

Fixed

  • [browser] Fix stacktrace test for node v10
  • [browser/node] Fix linting errors

1.0.6 (November 18, 2019)

1.0.4 (November 12, 2019)

1.0.3 (November 7, 2019)

1.0.2 (October 28, 2019)

1.0.1 (October 28, 2019)

1.0.0 (October 21, 2019)