TLDR: We rewrote our npm package, and now it's awesome. Check out the code on GitHub


Node.js was one of the very first languages supported by Honeybadger after Ruby and Client-Side JavaScript. At the time, one of our customers had written a simple npm package which we ended up officially adopting and maintaining. That package served us well for several years, but lately we've been wishing it could do more. A lot more. Things like reporting errors from AWS Lambda, or plugging into frameworks like Connect and Express. Reporting errors in asynchronous callbacks was a must, too, and automatically reporting uncaught errors when your processes crash.

So, we wrote a completely new package for Node; one which does all of those things we wished for, and has an API similar to our clients for other languages. At the same time, it's distinctly JavaScript. Where our Ruby library follows a more object-oriented approach, we went a little more functional in style for Node. The code is concise, modular, and tries to make few assumptions about how you want to use it. While we do automatically hook into Node's global uncaughtException event, the other integrations are provided as optional middleware. And of course, if you just want to report errors manually, it's as easy as Honeybadger.notify(error).

So what's changed?

In short, everything. But luckily, if you're using the older version of our npm package (< 1.0.0), upgrading should be pretty straightforward. Basically, instead of this (0.x syntax):

var Honeybadger = require('honeybadger');
var hb = new Honeybadger({
  apiKey: '[ YOUR API KEY HERE ]',
});
hb.send(err);

Do this (1.0 syntax):

var hb = require('honeybadger').configure({
  apiKey: '[ YOUR API KEY HERE ]'
});
hb.notify(err);

The new Honeybadger.notify() function should take all the same arguments as the old one, but is now even more powerful due to extra arguments and callbacks (more on that later).

If you were initializing multiple clients before, see the new Honeybadger.factory() function which returns a completely new Honeybadger object which can be configured separately from the global singleton.

The rest of this post will focus on some of the new features we've built into honeybadger v1.0.

Connect and Express middleware

Errors which happen in Express or Connect apps can be automatically reported to Honeybadger by installing our new middleware:

app.use(Honeybadger.requestHandler); // Use *before* all other app middleware.
// app.use(myMiddleware);
app.use(Honeybadger.errorHandler);  // Use *after* all other app middleware.
// app.use(myErrorMiddleware);

We've also created a sample of reporting errors from Express which you can deploy to Heroku with a single click:

Deploy

Reporting errors from AWS Lambda

Honeybadger can also automatically report errors which happen in AWS Lambda functions, which is something we recently blogged about. We're super excited to bring this feature to Honeybadger for Node! It's as simple as:

// Your handler function.
function handler(event, context) {
  console.log('Event:', event);
  console.log('Context:', context);
  throw(new Error('Something went wrong.'));
  console.log("Shouldn't make it here.");
}

// Build and export the function.
exports.handler = Honeybadger.lambdaHandler(handler);

Take a look at our example of reporting errors from AWS Lambda to learn how to deploy your function with Honeybadger.

Uncaught exception handler

By default Honeybadger will now report all unhandled errors which crash your node processes. Here's an example:

// crash.js
var Honeybadger = require('./lib/honeybadger').configure({
  apiKey: 'YOUR API KEY HERE'
});
Honeybadger.logger.level = 'info';

throw(new Error('Uncaught error, exiting.'));

...results in:

$ node crash.js
POST to honeybadger successful { id: '11e63938-8e7f-48df-8b23-b8390c299e43' }
Error: Uncaught error, exiting.
    at Object.<anonymous> (/Users/josh/code/honeybadger-node/test.js:7:7)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)
    at node.js:404:3

New Honeybadger.notify() callback

Honeybadger.notify() now accepts more arguments than before, including an optional callback function as the last argument:

Honeybadger.notify(err, function notifyCallback(err, notice) {
  if (err) {
    return console.error(err);
  }
  // If there was no error, log the notice:
  console.log(notice); // { id: 'uuid' }
});

This allows you to do things with the UUID of the error returned by our server after an error is reported. The UUID can be used to find the error in the Honeybadger app, or (eventually) associate user feedback with the error in Honeybadger.

For more examples of using Honeybadger.notify(), check out the documentation.

At Honeybadger we ♥ Node

Those were just a few of our favorite features of the new Honeybadger for Node. Be sure to check out even more of the new stuff we've added:

We're excited to bring even more awesome features to Node in the future. Next up we want to add the ability to notify Honeybadger when you deploy your Node app natively from the honeybadger package.


Feedback is welcome! Have a comment, question, suggestion, or bug report? Email me, submit an issue, or send a pull request.

Need a Honeybadger account? Start your free trial and exterminate your Node errors today!

Try Honeybadger for FREE

Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
Start free trial
Easy 5-minute setup — No credit card required
author photo
Joshua Wood

Josh started hacking on IRC scripts in the late nineties and has been hacking on computers (and businesses) ever since. As a co-founder of Honeybadger he works on the product, leads marketing and developer relations, and is involved in many open-source projects.

More articles by Joshua Wood