sentry-javascript
sentry-javascript copied to clipboard
[v8][node][express] setupExpressErrorHandler doesn't catch the errors
Is there an existing issue for this?
- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [X] I have reviewed the documentation https://docs.sentry.io/
- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/node
SDK Version
8.2.1
Framework Version
express.js
Link to Sentry event
No response
SDK Setup
No response
Steps to Reproduce
If there is a similar router as below, then setupExpressErrorHandler does not work.
app.use((error: Error, req: Request, res: Response, next: NextFunction) => {
return error ? res.status(400).send(error.message).end() : next();
});
Sentry.setupExpressErrorHandler(app);
We can swap them as a workaround. In this case, the Sentry will catch errors, and the response will be returned as expected.
Sentry.setupExpressErrorHandler(app);
app.use((error: Error, req: Request, res: Response, next: NextFunction) => {
return error ? res.status(400).send(error.message).end() : next();
});
Expected Result
work
Actual Result
doesn't work
this part made gave me good laugh 🤣
Hey,
yeah, sorry about that, we have not documented this ideally. You have to call setupExpressErrorHandler() after your routes are defined, but before all other middlewares. I'll fix this in the docs too!