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

Hapi errors are reported before onPreResponse

Open camsteffen opened this issue 1 year ago • 0 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.9.2

Framework Version

Hapi 21.3.10

Link to Sentry event

No response

SDK Setup/Reproduction Example

I did some debugging and found that the issue can be fixed by specifying the 'error' channel here, like this:

server.events.on({name:'request', channels: ['error']}, (request, event) => {

Otherwise, an internal channel event occurs and that event is apparently too soon in the request lifecycle. I figured this out by comparing with hapi-sentry (code).

I believe this is enough for a minimal reproducible example:

Sentry.init();
Sentry.setupHapiErrorHandler(server);
server.ext('onPreResponse', (response, h) => {
  throw Boom.notFound();
});

Steps to Reproduce

  1. Send a request where the route handler throws an Error
  2. The onPreResponse hook replaces the error with a Boom error

Expected Result

No error is reported to Sentry since Boom error responses do not generate Hapi response error events. And the error thrown from the handler should not be considered uncaught.

Actual Result

The Error thrown from the handler is reported to Sentry.

camsteffen avatar Jun 28 '24 20:06 camsteffen