sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

[v8][node][express] setupExpressErrorHandler doesn't catch the errors

Open xr0master opened this issue 1 year ago • 2 comments

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

xr0master avatar May 18 '24 06:05 xr0master

image

this part made gave me good laugh 🤣

amiranvarov avatar May 18 '24 19:05 amiranvarov

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!

mydea avatar May 21 '24 11:05 mydea