The raygun4node provider

The official Raygun provider for node.js allows you to easily send and track errors from your node.js network application.

This provider is flexible and supports many configuration scenarios - you can have it running and sending errors quickly, by manually placing send() calls when you detect an error. If you are using the Express.js web application framework, you can add the included middleware at the end of your middleware definitions. Or, you can use node.js domains.

The provider is available at the Raygun4node GitHub repository.

What can I send from my node.js application?

The provider can send Error objects, with error details (time, message etc) a stack trace, request data, environment data, custom user data and more. If you wish to manually send other information, the transport object has a Send function which will pass data to Raygun with the required fields.

Getting started

To add Raygun to your node.js app, follow these steps:

  1. In your project root, grab the module by running
    npm install raygun
  2. Create a RaygunClient using your application API key:

    var raygun = require('raygun');
    var raygunClient = new raygun.Client().init({ apiKey: '{{youApiKey}}' });
    

Sending errors

Using domains

A good way to trap and send exceptions, as it works with both synchronous and asynchronous code, is by using domains. In the code sample below, a test error is being thrown in the run function.

var d = require('domain').create();
d.on('error', function(err){
  raygunClient.send(err, {}, function () {
    process.exit();
  });
});

d.run(function(){
  var err = new Error('phasers offline');
  throw err;
});

Express.js

If you are using express.js to develop a web application, you can use the provided middleware as an error handler, at the bottom of your middleware definitions:

var raygunClient = new raygun.Client().init({apiKey: '{{yourApiKey}}'});

...
app.use(express.static(path.join(__dirname, 'public')));
app.use(raygunClient.expressHandler);
var err = new Error('help!');
throw err;

Manual sending

If you don't want to use domains, such as with in an older version of node.js, you can also wrap your code in a try block, then call raygunClient.send(err) in the catch block.

Documentation

For complete documentation including sending custom data, user tracking and version support, view the node.js documentation here.