Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
164 lines (131 sloc) 5.04 KB

Raven Build Status

Node v0.10 compatible

Log errors and stack traces in Sentry from within your Node.js applications. Includes middleware support for Connect/Express.

All processing and sending happens asynchronously to not slow things down if/when Sentry is down or slow.


  • 0.6.x
  • 0.8.x
  • 0.10.x


$ npm install raven

Basic Usage

var raven = require('raven');
var client = new raven.Client('{{ SENTRY_DSN }}');

client.captureMessage('Hello, world!');

Disable Raven

Pass false as the DSN (or any falsey value).

client = new raven.Client(process.env.NODE_ENV === 'production' && '{{ SENTRY_DSN }}')

Note: We don't infer this from NODE_ENV automatically anymore. It's up to you to implement whatever logic you'd like.

Logging an error

client.captureError(new Error('Broke!'));

Logging a query

client.captureQuery('SELECT * FROM `awesome`', 'mysql');

Sentry Identifier

client.captureMessage('Hello, world!', function(result) {
client.captureError(new Error('Broke!'), function(result) {

Note: client.captureMessage will also return the result directly without the need for a callback, such as: var result = client.captureMessage('Hello, world!');


If you really care if the event was logged or errored out, Client emits two events, logged and error:

client.on('logged', function(){
  console.log('Yay, it worked!');
client.on('error', function(e){
  console.log('oh well, Sentry is broke.');

Error Event

The event error is augmented with the original Sentry response object as well as the response body and statusCode for easier debugging.

client.on('error', function(e){
  console.log(e.responseBody);  // raw response body, usually contains a message explaining the failure
  console.log(e.statusCode);  // status code of the http request
  console.log(e.response);  // entire raw http response object

Environment variables


Optionally declare the DSN to use for the client through the environment. Initializing the client in your app won't require setting the DSN.


Optionally set the name for the client to use. What is name?


Optionally set the site for the client to use. What is site?

Catching global errors

For those times when you don't catch all errors in your application. ;)

// or
// or
raven.patchGlobal('{{ SENTRY_DSN }}');

It is recommended that you don't leave the process running after receiving an uncaughtException (, so an optional callback is provided to allow you to hook in something like:

client.patchGlobal(function() {
  console.log('Bye, bye, world.')

The callback is called after the event has been sent to the Sentry server.


new raven.Client(dsn[, options])
client.captureQuery(string, string[,callback])


Connect/Express middleware

The Raven middleware can be used as-is with either Connect or Express in the same way. Take note that in your middlewares, Raven must appear after your main handler to pick up any errors that may result from handling a request.


var connect = require('connect');
function mainHandler(req, res) {
  throw new Error('Broke!');
function onError(err, req, res, next) {
  // The error id is attached to `res.sentry` to be returned
  // and optionally displayed to the user for support.
  res.statusCode = 500;
  raven.middleware.connect('{{ SENTRY_DSN }}'),
  onError, // optional error handler if you want to display the error id to a user


var app = require('express').createServer();
app.use('{{ SENTRY_DSN }}'));
app.use(onError); // optional error handler if you want to display the error id to a user
app.get('/', function mainHandler(req, res) {
  throw new Error('Broke!');

Note: or raven.middleware.connect must be added to the middleware stack before any other error handling middlewares or there's a chance that the error will never get to Sentry.


You can find me on IRC. I troll in #sentry on freenode.

Something went wrong with that request. Please try again.