Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

honeybadger-js

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

honeybadger-js

A JavaScript library for integrating apps with the Honeybadger Rails Error Notifier.

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by0.63%
Maintainers
1
Weekly downloads
 
Created
Source

Honeybadger Client Side Javascript Library

Build
Status

A JavaScript library for integrating apps with the :zap: Honeybadger Rails Error Notifier.

Upgrading

It is recommended that you use our CDN, as outlined under installation. The API is 100% backwards compatible, so no other code changes are required.

Note: 0.1 and 0.2 make significant improvements to error grouping. As a result, new errors may be grouped differently than old.

Installation

Place the following code between the <head></head> tags of your page:

<script src="//js.honeybadger.io/v0.3/honeybadger.min.js" type="text/javascript"></script>
<script type="text/javascript">
  Honeybadger.configure({
    api_key: 'project api key',
    environment: 'production'
  });
</script>

Basic Usage

To catch an error and notify Honeybadger:

try {
  // ...error producing code...
} catch(e) {
  Honeybadger.notify(e);
}

JavaScript often uses generic class names -- such as Error -- which are uninformative and also cause unrelated errors to be grouped together. To get around this issue it's a good practice to send a custom error class when notifying Honeybadger:

try {
  // ...error producing code...
} catch(e) {
  Honeybadger.notify(e, 'DescriptiveClass');
}

Sending Custom Data

Honeybadger allows you to send custom data using Honeybadger.setContext And Honeybadger.resetContext:

// On load
Honeybadger.setContext({
  user_id: '<%= current_user.id %>'
});

// Later
Honeybadger.setContext({
  backbone_view: 'tracks'
});

// Honeybadger.context => { user_id: 1, backbone_view: 'tracks' }

Honeybadger.resetContext({
  some_other_data: 'foo'
});

// Honeybadger.context == { some_other_data: 'foo' }

You can also add context to a specific exception by passing an associative array to the notify method. Global context will be merged locally:

Honeybadger.setContext({
  user_id: '<%= current_user.id %>'
});

try {
  // ...error producing code...
} catch(e) {
  Honeybadger.notify(e, { context: { some_other_data: 'foo' } });
}

// Honeybadger.context == { user_id: 1 }

Notification handlers

Passing a function to Honeybadger.beforeNotify will add the function to a list of before notify handlers. If the function includes a parameter, the Notice object will be passed as an argument. Multiple handlers may be added in this fashion:

Honeybadger.beforeNotify(function(notice) {
  notice.message = 'My custom message';
});

To halt notification, return false from any beforeNotify handler:

Honeybadger.beforeNotify(function(notice) {
  if (notice.class == 'MyCustomError') return false;
});

Notice Attributes

The following notice attributes may be modified by your notification handlers:

  • stack - The stack trace
  • class - The exception class name
  • message - The error message
  • url - The current url
  • project_root - The root url
  • environment - Name of the environment. example: "production"
  • component - Similar to a rails controller name. example: "users"
  • action - Similar to a rails action name. example: "create"
  • fingerprint - A unique fingerprint, used to customize grouping of errors in Honeybadger.
  • context - The context object.

Sourcemaps

Honeybadger can automatically un-minify your code if you provide a sourcemap along with your minified JavaScript files.

In order to make this work you must include a special comment at the bottom of your minified file which points to the corresponding sourcemap file. To see what this comment should look like, see the comment at the bottom of honeybadger.js. If you upload the original source files along with the sourcemap, Honeybadger will link to those files when displaying the stack trace.

All files must be publically accessible online so that Honeybadger's servers can download and parse them. For the full specification, see the Source Map Revision 3 Proposal.

Linking stack traces to source files

Honeybadger supports an optional sourceRoot comment, which should point to the root path of the original source files online. For example:

// ...minified code...
//# sourceMappingURL=application.min.js.map
//# sourceRoot=https://sources.my-domain.com/src

This option may also be specified as a top-level key in the JSON sourcemap file itself:

{
  "sourceRoot" : "https://sources.my-domain.com/src",
  // ...sourcemap...
}

To customize this setting just for Honeybadger, use honeybadgerSourceRoot instead.

Linking stack traces to GitHub

If you're using the GitHub integration, you can also link to source files on GitHub by substituting a special [PROJECT_ROOT] token for the root of your GitHub repository:

// ...minified code...
//# sourceMappingURL=honeybadger.min.js.map
//# honeybadgerSourceRoot=[PROJECT_ROOT]/src

This is the only situation in which the source root is not required to be a valid URL.

Unhandled errors via (window.onerror)

By default, honeybadger.js does not track unhandled errors. This is because window.onerror is a very limited method of error handling, and does not usually yield useful information. It is our official recommendation to always use try/catch explicitly to notify Honeybadger. If you still want to automatically catch errors via window.onerror, you can set the onerror configuration option to true:

Honeybadger.configure({
  api_key: 'project api key',
  onerror: true
});

Configuration

Honeybadger.configure may be called multiple times to set/update configuration options. Existing configuration will be merged. In most cases configuration will be set once, however the action and component options may change semi-frequently for client-side frameworks like Angular or Ember.

Honeybadger.configure({
  // Honeybadger API key (required)
  api_key: '',

  // Collector Host
  host: 'api.honeybadger.io',

  // Use SSL?
  ssl: true,

  // Project root
  project_root: 'http://my-app.com',

  // Environment
  environment: 'production',

  // Component (optional)
  component: '',

  // Action (optional)
  action: '',

  // Should unhandled (window.onerror) notifications be sent?
  onerror: false,

  // Disable notifications?
  disabled: false,

  // Timeout (in milliseconds) when making requests.
  timeout: false
});

Contributing

  1. Fork it.
  2. Create a topic branch git checkout -b my_branch
  3. Commit your changes git commit -am "Boom"
  4. Push to your branch git push origin my_branch
  5. Send a pull request

Running the tests

To run the test suite, enter make test into the console.

License

The Honeybadger gem is MIT licensed. See the MIT-LICENSE file in this repository for details.

FAQs

Package last updated on 17 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc