Node v0.9 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.9.x (latest unstable)
$ npm install raven
var raven = require('raven');
var client = new raven.Client('{{ SENTRY_DSN }}');
client.captureMessage('Hello, world!');
Run with:
$ NODE_ENV=production node script.js
client.captureError(new Error('Broke!'));
client.captureQuery('SELECT * FROM `awesome`', 'mysql');
client.captureMessage('Hello, world!', function(result) {
console.log(client.getIdent(result));
});
client.captureError(new Error('Broke!'), function(result) {
console.log(client.getIdent(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(){
console.log('oh well, Sentry is broke.');
})
client.captureMessage('Boom');
NODE_ENV
must be set to production
for Sentry to actually work. Without being in production, a warning is issued and logging disabled.
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?
For those times when you don't catch all errors in your application. ;)
client.patchGlobal();
// or
raven.patchGlobal(client);
// or
raven.patchGlobal('{{ SENTRY_DSN }}');
It is recommended that you don't leave the process running after receiving an uncaughtException
(http://nodejs.org/api/process.html#process_event_uncaughtexception), so an optional callback is provided to allow you to hook in something like:
client.patchGlobal(function() {
console.log('Bye, bye, world.')
process.exit(1);
});
The callback is called after the event has been sent to the Sentry server.
new raven.Client(dsn[, options])
client.captureMessage(string[,callback])
client.captureError(Error[,callback])
client.captureQuery(string, string[,callback])
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;
res.end(res.sentry+'\n');
}
connect(
connect.bodyParser(),
connect.cookieParser(),
mainHandler,
raven.middleware.connect('{{ SENTRY_DSN }}'),
onError, // optional error handler if you want to display the error id to a user
).listen(3000);
var app = require('express').createServer();
app.error(raven.middleware.express('{{ SENTRY_DSN }}'));
app.error(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!');
});
app.listen(3000);
You can find me on IRC. I troll in #sentry
on freenode
.