Description
Is there an existing issue for this?
- I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- I have reviewed the documentation https://docs.sentry.io/
- 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.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
Type
Projects
Status