Skip to content

Sentry reports even handled errors in Fastify Β #12055

Closed as not planned
Closed as not planned
@jillro

Description

@jillro

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.0.0

Framework Version

4.27.0

Link to Sentry event

No response

SDK Setup

No response

Steps to Reproduce

const Sentry = require('@sentry/node')
Sentry.init(...)

const app = require('fastify')()

Sentry.setupFastifyErrorHandler(app)

app.setErrorHandler((error, req, res) => {
  if (error.message === 'unauthorized') {
    return res.code(401).send('unauthorized')
  }

  throw error
})

app.get('/should_not_be_reported', {
  onRequest: async () => {
    throw Error('unauthorized')
  }
}, async (req, res) => {
  res.send('OK')
})

app.get('/should_be_reported', {
  onRequest: async () => {
    throw Error('unhandled error')
  }
}, async (req, res) => {
  res.send('OK')
})

Expected Result

Only the second error (unhandled error) should be reported.

Actual Result

Both error are reported.

Sentry uses Fastify's onError hooks to catch errors. Fastify documentation states that This hook will be executed only after the Custom Error Handler set by setErrorHandler has been executed, and only if the custom error handler sends an error back to the user (Note that the default error handler always sends the error back to the user).

But undocumented behavior is that errors thrown from other hooks, even if finally sent to the custom error handler, go to onError hook before going the error handler. This cause every error thrown in other hooks to be reported by Sentry even before it can be handled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nodeIssues related to the Sentry Node SDKStale

    Type

    Projects

    Status

    Waiting for: Community

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions