GithubHelp home page GithubHelp logo

jbelke / honeybadger-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from honeybadger-io/honeybadger-js

0.0 3.0 0.0 536 KB

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

Home Page: http://www.honeybadger.io/

License: MIT License

JavaScript 78.97% Makefile 1.67% HTML 19.36%

honeybadger-js's Introduction

Honeybadger Client-Side Javascript Library

Build Status

A client-side JavaScript library for integrating apps with the โšก Honeybadger Error Notifier. For server-side javascript, check out our NodeJS library.

Getting Started

1. Include the JS library

Globally

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

<script src="//js.honeybadger.io/v0.4/honeybadger.min.js" type="text/javascript" data-api_key="project api key" data-environment="production"></script>

Honeybadger may also be configured via JavaScript:

<script type="text/javascript">
  Honeybadger.configure({
    api_key: 'project api key',
    environment: 'production'
  });
</script>

Here's a video walkthrough of a basic, global installation:

Using Honeybadger with Javascript

Installing via Node.js

npm install honeybadger-js --save-dev

Installing via Bower

bower install honeybadger --save-dev

Browserify/Webpack (CommonJS)

var Honeybadger = require("path/to/honeybadger");
Honeybadger.configure({
  api_key: 'project api key',
  environment: 'production'
});

RequireJS (AMD)

requirejs(["path/to/honeybadger"], function(Honeybadger) {
  Honeybadger.configure({
    api_key: 'project api key',
    environment: 'production'
  });
});

Rails Assets

Note: First, make sure you use bundler >= 1.8.4.

Add the following to your Gemfile:

source 'https://rails-assets.org' do
  gem 'rails-assets-honeybadger'
end

Add the following to application.js:

//= require honeybadger

Honeybadger.configure({
  api_key: 'project api key',
  environment: 'production'
});

2. Start reporting exceptions

By default Honeybadger will report all uncaught exceptions automatically using our window.onerror handler.

You can also manually notify Honeybadger of errors and other events in your application code:

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

See the full documentation for the notify method for more examples.

Advanced Configuration

You can set configuration options by using the Honeybadger.configure function. All of the available options are shown below:

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: true,

  // Disable notifications?
  disabled: false
});

You can call Honeybadger.configure as many times as you like. The existing configuration data will be merged with any new data you provide. This is especially useful for changing the action and component values inside of single-page apps.

Configuring via data attributes

The global Honeybadger instance may also be configured via data attributes on the script tag which loads honeybadger.js:

<script src="honeybadger.js" type="text/javascript" data-component="pages" data-action="index" ...></script>

Public Interface

Honeybadger.notify(): Send an exception to Honeybadger

If you've caught an exception and want to send it to Honeybadger, this is the method for you.

Examples:

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

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:

Honeybadger.notify(error, 'DescriptiveClass');

You can also set or override other optional data which is reported with the error:

Honeybadger.notify(error, {
  message: 'My custom message',
  name: 'DescriptiveClass',
  component: 'badgers',
  action: 'show',
  context: { badgerId: 1 },
  fingerprint: 'This unique string will group similar errors together',
  environment: 'production',
  project_root: 'https://www.example.com/'
});

Finally, you can notify Honeybadger of anything, even if you don't have an error object:

Honeybadger.notify('Badgers!');
Honeybadger.notify('Badgers!', { ... });
Honeybadger.notify('Badgers!', 'CustomClass');
Honeybadger.notify('Badgers!', 'CustomClass', { ... });
Honeybadger.notify({
  message: 'Badgers!',
  name: 'CustomClass',
  ...
});

A stacktrace will be generated for you (when possible) if you do not provide an error object.


Honeybadger.wrap(): Wrap the given function in try/catch and report any exceptions

It can be a pain to include try/catch blocks everywhere in your app. A slightly nicer option is to use Honeybadger.wrap. You pass it a function. It returns a new function which wraps your existing function in a try/catch block.

Examples:

Honeybadger.wrap(function(){
  throw "oops";
})();

Note that wrap returns a function. This makes it easy to use with event handlers, as in the example below:

$(document).on("click", "#myElement", Honeybadger.wrap(function(){ throw "oops"; }));

Honeybadger.setContext(): Set metadata to be sent if an exception occurs

Javascript exceptions are pretty bare-bones. You probably have some additional data that could make them a lot easier to understand - perhaps the name of the current Angular view, or the id of the current user. This function lets you set context data that will be sent if an error should occur.

You can call setContext as many times as you like. New context data will be merged with the existing data.

Examples:

// On load
Honeybadger.setContext({
  user_id: 123
});

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

// The context now contains { user_id: 123, backbone_view: 'tracks' }

Honeybadger.resetContext(): Clear context metadata

If you've used Honeybadger.setContext to store context data, you can clear it with Honeybadger.resetContext.

Example:

// Set the context to {}
Honeybadger.resetContext();

// Clear the context, then set it to `{ user_id: 123 }`
Honeybadger.resetContext({
  user_id: 123
});

Honeybadger.beforeNotify(): Add a callback to be run before an exception is reported

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:

Examples

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

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

  • stack - The stack trace
  • name - 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.

Honeybadger.configure(): Set configuration values

The configure method takes an object containing config values. Its return value is unspecified.

Examples:

Honeybadger.configure({api_key: "adlkjfljk"});

Honeybadger.factory(): create a new client instance.

The factory method returns a new instance of Honeybadger which can be configured differently than the global/singleton instance.

Examples:

var other_hb = Honeybadger.factory({api_key: "zxcvbnm"});
other_hb.notify("This will go to an alternate project.");

Sourcemaps

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

To do this, you'll add a special comment at the bottom of your minified JS. It tells us where to find your sourcemap. For example:

// ...minified code...
//# sourceMappingURL=application.min.js.map

The sourcemap URL needs to be a valid URL accessible to the public.

For more information on sourcemaps, check out the Source Map Revision 3 Proposal.

Linking stack traces to source files

If you'd like to be able to jump from the Honeybadger backtrace to your unminified source file, just tell us where to find your unminified files using the sourceRoot option.

sourceRoot is the root URL for your unminified source files. To set it, you can use another magic comment:

// ...minified code...
//# 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...
}

If providing the sourceRoot option fouls up other tools in your toolchain, you can alternatively use honeybadgerSourceRoot.

Using GitHub

If you're using Honeybadger's GitHub integration, you can 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.

window.onerror

Honeybadger.js automatically reports uncaught exceptions from window.onerror. To disable notifications for uncaught exceptions, set the onerror option to false.

Honeybadger.configure({
  api_key: 'project api key',
  onerror: 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.

honeybadger-js's People

Contributors

joshuap avatar starrhorne avatar scottmessinger avatar tomfuertes avatar

Watchers

James Cloos avatar Joshua Belke avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.