Honeybadger Client Side Javascript Library
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 {
} 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 {
} catch(e) {
Honeybadger.notify(e, 'DescriptiveClass');
}
Sending Custom Data
Honeybadger allows you to send custom data using
Honeybadger.setContext
And Honeybadger.resetContext
:
Honeybadger.setContext({
user_id: '<%= current_user.id %>'
});
Honeybadger.setContext({
backbone_view: 'tracks'
});
Honeybadger.resetContext({
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 {
} catch(e) {
Honeybadger.notify(e, { context: { some_other_data: 'foo' } });
}
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:
This option may also be specified as a top-level key in the JSON sourcemap file itself:
{
"sourceRoot" : "https://sources.my-domain.com/src",
}
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:
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({
api_key: '',
host: 'api.honeybadger.io',
ssl: true,
project_root: 'http://my-app.com',
environment: 'production',
component: '',
action: '',
onerror: false,
disabled: false,
timeout: false
});
Contributing
- Fork it.
- Create a topic branch
git checkout -b my_branch
- Commit your changes
git commit -am "Boom"
- Push to your branch
git push origin my_branch
- 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.