honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
334 stars 109 forks source link

Sentry middleware captures HTTPException #490

Open alexgleason opened 2 months ago

alexgleason commented 2 months ago

Hello! We are throwing an HTTPException in our code. hono/sentry captures this and creates an event in Sentry:

image

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.

alexgleason commented 2 months ago

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:

image

yusukebe commented 2 months ago

@sam-lippert Can you see it?

dihmeetree commented 1 week ago

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.