Airbrake and ActionHero

actionhero javascript node.js 
Thu Feb 13 2014


If you are using ActionHero version 10x or higher, you can now use the plugin:

evantahler/ah-airbrake-plugin

At work we use Airbrake to monitor exceptions in production. It’s a great tool that sends an email when something goes wrong (among other things). Now that we are using ActionHero in production, I wanted to integrate airbrake. To do this, I released version 7.6.5 which adds the ability to create custom error reporters.

ActionHero already worked all requests and tasks within a domain and reported on them, but that reporting was limited to the output of the winston logger. Winston has some great plugins, but airbrake was not one of them. Once we had custom error reporting support, I moved the initial winston logger (using api.log) to the new format:

1var consoleReporter = function (type, err, extraMessages, severity) { 2 for (var i in extraMessages) { 3 var line = extraMessages[i]; 4 api.log(line, severity); 5 } 6 var lines = err.stack.split(os.EOL); 7 for (var i in lines) { 8 var line = lines[i]; 9 api.log("! " + line, severity); 10 } 11 api.log("*", severity); 12}; 13 14api.exceptionHandlers.reporters.push(consoleReporter);

Felix has made a great airbrake library for node, which is simple to use with ActionHero’s new error reporter:

1// in initializers/airbrake.js 2 3var airbrakePrototype = require("airbrake"); 4 5exports.airbrake = function (api, next) { 6 api.airbrake = {}; 7 api.airbrake.token = api.config.general.airbrake_token; 8 api.airbrake.client = airbrakePrototype.createClient(api.airbrake.token); 9 api.airbrake.client.handleExceptions(); // catch global uncaught errors 10 // api.airbrake.client.developmentEnvironments = []; // don't report in various NODE_ENVs 11 12 api.airbrake.notifier = function (type, err, extraMessages, severity) { 13 api.airbrake.client.notify(err); 14 }; 15 16 api.airbrake._start = function (api, next) { 17 api.exceptionHandlers.reporters.push(api.airbrake.notifier); 18 next(); 19 }; 20 21 api.airbrake._stop = function (api, next) { 22 next(); 23 }; 24 25 next(); 26};

The last peace of the puzzle was informing airbrake about deployments, and clearing the previous errors. The airbrake package already has support for this, so we just needed to make a grunt task we call on deployment:

1// in /guntfile.js 2 3grunt.registerTask( 4 "notifyAirbrakeDeploy", 5 "tell airbrake we deployed", 6 function (message) { 7 var done = this.async(); 8 init(function (api) { 9 api.airbrake.client.trackDeployment(function () { 10 done(); 11 }); 12 }); 13 } 14);
Hi, I'm Evan

I write about Technology, Software, and Startups. I use my Product Management, Software Engineering, and Leadership skills to build teams that create world-class digital products.

Get in touch