Sentry middleware captures HTTPException
Hello! We are throwing an HTTPException in our code. hono/sentry captures this and creates an event in Sentry:
Is this the expected behavior? To me it seems that HTTPException shouldn't be captured, since it's actually a "handled" exception, in the sense that we are returning it manually (as an "escape-hatch") because we expect it to occur.
We are trying to work around this by catching the error, but it's actually an instance of Error and not HTTPException, so neither of these solutions work:
app.use('*', sentryMiddleware({ dsn: Conf.sentryDsn, ignoreErrors: 'HTTPException' }));
app.onError((err) => {
if (err instanceof HTTPException) {
return err.getResponse();
}
throw err;
});
It's just a plain Error:
@sam-lippert Can you see it?
Also have this issue. I'm doing something like this
app.use(
'*',
sentry({
ignoreErrors: ['Unauthorized']
})
)
and throwing the error like so:
const user = c.get('user')
if (!user) {
throw new HTTPException(401, { message: 'Unauthorized' })
}
But for some reason the error is still being sent to Sentry.
Bump! Any ideas @yusukebe @sam-lippert 🙂
@dihmeetree This might be an issue with the toucan-js library, since the ignoreErrors property should pass through to it. Do you have the same issue if you try to use Toucan directly?
There's also an update to the @hono/sentry middleware that can be applied to see if that works.
@alexgleason It looks like ignoreErrors filters on the error's message property according to Sentry's docs. Does filtering work for you if you update the middleware version and set ignoreErrors: ['No pubkey provided']?
I have the same problem and is eating my sentry quota away :/
Having the same issue! We have different error messages keys when throwing the HTTPException. Would also love to filter out those errors by status code (x>=400, x<500).
~Maybe creating a custom HTTPException that does not extend an Error instance could work?:~
export declare class HTTPException {
readonly res?: Response;
readonly status: StatusCode;
constructor(status?: StatusCode, options?: HTTPExceptionOptions);
getResponse(): Response;
}
Edit: It won't work.
Should be solved in #793. Allowing you to include which status codes from HTTPExceptions should be reported to Sentry:
sentry({
dsn: 'https://[email protected]/xxxxxx',
// Includes only 500-599 status codes by default.
includeStatusCodes: [{ min: 500, max: 599 }],
// Or, you can combine a specific status code and a range.
// includeStatusCodes: [500, { min: 503, max: 599 }],
// If you want to exclude all HTTPExceptions, you can set an empty array.
})
By default, if the error is an HTTPException instance, only those with status codes greater than 500 are reported to Sentry.
PR is closed. But here are a few solutions suggested:
a) Clean the error
app.onError((err, c) => {
...
if (err instanceof HTTPException) {
// your custom logic to clear any particular `HttpException`s
c.error = undefined
}
...
})
b) Disable sentry
app.onError((err, c) => {
...
if (err instanceof HTTPException) {
// your custom logic to clear any particular `HttpException`s
c.get('sentry').setEnabled(false)
}
...
})
Both work if you don't have custom logic that depends on the error down the track.
See the PR for Sam's feedback in https://github.com/honojs/middleware/pull/793#discussion_r1817685914
how do people work around this? I tried to apply the workarounds from the previous message and I still see the errors being reported to sentry:
const app = new Hono<AppType>()
app.onError((err, c) => {
if (err instanceof HTTPException) {
c.error = undefined
return c.json({ error: err.message }, err.status)
}
return c.json({ error: err.message }, 500)
})
app.use("*", sentry())
The solution is to not use this middleware. Just install Sentry like normal and call it where you need it.
@yusukebe Sounds like the author has accepted a solution, can you close this?
@sam-lippert Okay. Closing.