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.
$ npm install raven
var raven = require('raven');
var client = new raven.Client('{{ SENTRY_DSN }}');
client.createFromText('Hello, world!');
client.createFromError(new Error('Broke!'));
client.createFromText('Hello, world!', function(result) {
console.log(client.getIdent(result));
});
client.createFromError(new Error('Broke!'), function(result) {
console.log(client.getIdent(result));
});
Note: client.createFromText
will also return the result directly without the need for a callback, such as: var result = client.createFromText('Hello, world!');
For those times when you don't catch all errors in your application. ;)
client.patchGlobals();
// or
raven.patchGlobals(client);
// or
raven.patchGlobals('{{ SENTRY_DSN }}');
new raven.Client(dsn[, options])
client.createFromText(string[,callback])
client.createFromError(Error[,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);
- Support for process.env.SENTRY_DSN
- More complete test coverage
- Travis CI
- More comments in code
- More third party integration
- Logging support for SQL