I set up Sentry in my node.js app. Sentry catches only errors explicitly sent through Sentry.captureException but errors from Express router aren’t sent.
Here is a snippet of the implementation:
const Sentry = require('@sentry/node');
const app = express();
Sentry.init(); //conf in .env
app.use(Sentry.Handlers.requestHandler());
app.use('/api/v1', require('./app/routes'));
app.use(Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// Capture all 404 and 500 errors
if (error.status === 404 || error.status === 500) {
return true
}
return false
}
}));
app.get('/debug-sentry', function mainHandler(req, res) {
throw new Error('My first Sentry error!');
});
app.get('*', (req, res) => {
res.sendStatus(404);
});
Nor calling /debug-sentry or a non existing route sends an error to Sentry. Can so tells me what I’m doing wrong here?
Thanks!
I think shouldHandleError() might be broken. I was having the same issue but at least the /debug-sentry path works if I use the following error handler without shouldHandleError() at all:
app.use(Sentry.Handlers.errorHandler());
Nothing changes for me when deleting shouldHandleError from the error handler, pretty weird…
Are there by any chance any sdk logs to investigate this issue?
@cartman put the error handler to the file bottom after all other handlers.
const Sentry = require('@sentry/node');
const app = express();
Sentry.init(); //conf in .env
app.use(Sentry.Handlers.requestHandler());
app.use('/api/v1', require('./app/routes'));
app.get('/debug-sentry', function mainHandler(req, res) {
throw new Error('My first Sentry error!');
});
app.get('*', (req, res) => {
res.sendStatus(404);
});
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// Capture all 404 and 500 errors
if (error.status === 404 || error.status === 500) {
return true
}
return false
}
}));