Rollbar - Automated Error Logging for Node Apps

Posted Nov 11, 2016
by Rivkah Standig, Support Engineer/Developer Advocate at Rollbar

While you're trying to win Node Knockout, debugging and chasing down mysterious errors in your app is the last thing you want to do. Don't worry about unknown errors in your Node app; use the Node.js library for reporting errors to Rollbar, and rest easy knowing your errors are automatically logged and reported.

Getting Started

To get started with Rollbar, you'll need to sign up for an account. Node Knockout participants get 100,000 error events tracked for free, so you can see if Rollbar is right for you. And there's a bonus for Node Knockout winners in addition to the 100,000 free events: you'll get a Star Wars BB-8 Droid from Rollbar! :)

Once you've signed up for an account, you'll create a project. Since we're using Node, we'll select that for our language.

[]

Basic Node Integration

First, install Rollbar via npm:

$ npm install --save rollbar

Then, in your main application, require and initialize using your access token. Be sure to replace POST_SERVER_ITEM_ACCESS_TOKEN with your project's post_server_item access token, which you can find in the Rollbar.com interface.

var rollbar = require("rollbar");
rollbar.init("POST_SERVER_ITEM_ACCESS_TOKEN");

Other options can be passed in to the init() function using a second parameter. For example, to configure the library to send errors to api.rollbar.com:

rollbar.init("POST_SERVER_ITEM_ACCESS_TOKEN", {
  environment: "staging",
  endpoint: "https://api.rollbar.com/api/1/"
});

Next, record a generic message and send it to Rollbar:

rollbar.reportMessage("Hello world!");

To confirm that it worked, you can check and see if the exception appeared in the Rollbar dashboard.

Using Express

var express = require('express');
var rollbar = require('rollbar');

var app = express();

app.get('/', function(req, res) {
  // ...
});

// Use the rollbar error handler to send exceptions to your Rollbar account
app.use(rollbar.errorHandler('POST_SERVER_ITEM_ACCESS_TOKEN'));

app.listen(6943);

Uncaught Exceptions

Rollbar can be registered as a handler for any uncaught exceptions in your Node app.

var options = {
  exitOnUncaughtException: true
};
rollbar.handleUncaughtExceptions("POST_SERVER_ITEM_ACCESS_TOKEN", options);

Caught Exceptions

To report an exception that you have caught, you may use either handleError or the handleErrorWithPayloadData.

var rollbar = require('rollbar');
rollbar.init('POST_SERVER_ITEM_ACCESS_TOKEN');

try {
  someCode();
} catch (e) {
  rollbar.handleError(e);

  // if you have a request object (or a function that returns one), pass it as the second arg
  // see below for details about what the request object is expected to be
  rollbar.handleError(e, request);

  // you can also pass a callback, which will be called upon success/failure
  rollbar.handleError(e, function(err2) {
    if (err2) {
      // an error occurred
    } else {
      // success
    }
  });

  // if you have a request and a callback, pass the callback last
  rollbar.handleError(e, request, callback);

  // to specify payload options - like extra data, or the level - use handleErrorWithPayloadData
  rollbar.handleErrorWithPayloadData(e, {level: "warning", custom: {someKey: "arbitrary value"}});

  // can also take request and callback, like handleError:
  rollbar.handleErrorWithPayloadData(e, {level: "info"}, request);
  rollbar.handleErrorWithPayloadData(e, {level: "info"}, callback);
  rollbar.handleErrorWithPayloadData(e, {level: "info"}, request, callback);
}

For further information, you'll want to check out Rollbar's docs for information on advanced ways to use the Node library, as well as information on log messages, unhandled rejections, person tracking, and more.

Working with Rollbar

Now that you have Rollbar integrated into your Node app, any errors that occur will be automatically captured and viewable from your Rollbar Dashboard and Error Items page. You'll be able to easily see what errors are occurring, how often they occur and the full context and analytics into your exceptions.

Rollbar provides a full stack trace of every exception allowing you to see exactly what's happening in your application when an error occurs. You can also view the user's browser and OS where the error occurred.

Errors are grouped by frequency so that you can prioritize which ones to work on first. You'll be able to view trends over time. If a certain error spikes in frequency, you'll know about it right away.

Rollbar also allows you to manually handle errors in your application and prioritize which errors are the most critical to your application's health.

Moving Forward

You can rest assured that you'll know about any bugs that might make their into your Node Knockout project. We're excited to sponsor Node Knockout and can't wait to see all of the amazing projects that are going to be built! For more information about the Node library, view the docs. Happy hacking!