JS in Rails
Installing airbrake-js in a Rails application
key Features
- Easy and flexible installation options including npm and Yarn
- Send uncaught errors to Airbrake or manually using a try/catch
- Add custom parameters to your errors for more context
- Private source map support
- Control which errors you send with customizable filtering options
Installation & Configuration
Option 1 - Asset pipeline
Copy the latest compiled UMD package bundle from
https://unpkg.com/@airbrake/browser to
vendor/assets/javascripts/airbrake.js
in your project.
Then, add the following code to your Sprockets manifest:
//= require airbrake
var airbrake = new Airbrake.Notifier({
projectId: 1,
projectKey: 'FIXME'
});
airbrake.addFilter(function(notice) {
notice.context.environment = "<%= Rails.env %>";
return notice;
});
try {
throw new Error('Hello from Airbrake!');
} catch (err) {
airbrake.notify(err).then(function(notice) {
if (notice.id) {
console.log('notice id:', notice.id);
} else {
console.log('notify failed:', notice.error);
}
});
}
Option 2 - Webpacker
Add @airbrake/broswer
to your application.
yarn add @airbrake/browser
In your main application pack, import @airbrake/browser
and configure the client.
import { Notifier } from '@airbrake/browser';
const airbrake = new Notifier({
projectId: 1,
projectKey: 'FIXME'
});
airbrake.addFilter((notice) => {
notice.context.environment = process.env.RAILS_ENV;
return notice;
});
try {
throw new Error('Hello from Airbrake!');
} catch (err) {
airbrake.notify(err).then((notice) => {
if (notice.id) {
console.log('notice id:', notice.id);
} else {
console.log('notify failed:', notice.error);
}
});
}
You should now be able to capture JavaScript exceptions in your Ruby on Rails application.
Troubleshoot
Installation and configuration is just the beginning. The airbrake-js notifier supports many other advanced uses and options including:
- adding extra details to errors
- source maps for easy to parse backtraces
- filtering errors
- specifying error severity
Please visit the airbrake-js GitHub repo for more usage and configuration examples.