Raygun4Node – Node.js provider now available

| 2 min. (370 words)

A lot of you have been clamouring for a Node.js provider for Raygun and I am happy to announce that an early build of it is now available and is on NPM. You can check out the source code and raise issues and submit pull requests at the GitHub repository for it, here.

You can install it using: `npm install raygun`

The following code shows how to create and set you API key:

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

After this you have some options for how you wish to use the Raygun Node provider. You can manually send errors through to Raygun as below:

try {
    // the synchronous code that we want to catch thrown errors on
    var err = new Error('warp drive failing');
    throw err;
} catch (err) {
    raygunClient.send(err);
}

If you are using domains then you can use the following code:

var d = require('domain').create();
d.on('error', function(err){
    var killtimer = setTimeout(function() {
        process.exit(1);
     }, 5000);

    killtimer.unref();
    raygunClient.send(err);
    process.exit();
});

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

If you wish to add it as a global exception handler you can do the following:

// do not use this in modules, but only in applications
process.on('uncaughtException', function(err) {
    raygunClient.send(err);
});

Be careful with that last one as this can leave your process in an unknown state. You should know that as soon as you register the above handler then the default behaviour of Node of stopping the process will cease. Have a read of the Node documentation on this here so that you know the full story.

There is also a hook if you are using Express.js, this can be used as follows:

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

var raygunClient = new raygun.Client().init({ apiKey: 'your API key' });
var app = express();

// define some routes here

app.use(raygunClient.expressHandler);

Here is an example error that we logged to Raygun with the Node.js provider

Be sure to raise any issues or ideas on GitHub, looking forward to working more on this one.

So if you are using Node.js and want to give Raygun a spin, sign up now for a free trial

Happy error blasting!