sentry-javascript
sentry-javascript copied to clipboard
Sentry reports even handled errors in Fastify
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.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.
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 🤔
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community
, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀