Skip to content

Hapi errors are reported before onPreResponse #12702

@camsteffen

Description

@camsteffen

Is there an existing issue for this?

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.

Metadata

Metadata

Assignees

Labels

Package: nodeIssues related to the Sentry Node SDK

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions