Honeybadger for Node.js is now awesome

We recently completely rewrote our npm package, adding new features such as a native AWS Lambda error handler, Express/Connect support, and more!

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!

What to do next:
  1. 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
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Joshua Wood

    Josh is Honeybadger's resident bug hunter and technical debt collector. Once enthusiastically referred to as a "human exception tracker", he spends his days crafting the middleware, plugins, and gems which keep the 'badger fat and happy through a steady diet of fresh data.

    More articles by Joshua Wood
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial