Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry reports even handled errors in Fastify #12055

Open
3 tasks done
jillro opened this issue May 15, 2024 · 1 comment
Open
3 tasks done

Sentry reports even handled errors in Fastify #12055

jillro opened this issue May 15, 2024 · 1 comment

Comments

@jillro
Copy link

jillro commented May 15, 2024

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.

@mydea
Copy link
Member

mydea commented May 16, 2024

Hey, thank you for raising this!

You identified the problem well, awesome - sadly, I think there isn't much we can do there except hope to get this fixed in fastify itself - let's see what folks over there in the issue you opened answer. We can possibly look into contributing a fix ourselves, but we'd need to get into the fastify codebase first as well.

From the top of my head, I don't know what we can do to "fix" this easily today 🤔 One thing you could do (which is a workaround, obviously, but could make it work for you) is to configure this in beforeSend in addition, to filter out these exceptions there as well - see https://docs.sentry.io/platforms/javascript/guides/node/configuration/filtering/#event-hints:

Sentry.init({
  beforeSend(event, hint) {
    const error = hint.originalException;
    if (error && error.message && error.message === 'unauthorized') {
      return null;
    }
  }
})

To avoid these being sent. This is of course not ideal because you need to kind of duplicate this logic, but maybe you can also find a way to extract this out into a utility somehow 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for: Community
Development

No branches or pull requests

3 participants