Skip to content

NestJS SentryExeceptionCaptured doesn't catch custom filtersΒ #14580

@adamghowiba

Description

@adamghowiba

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nestjs

SDK Version

8.40.0

Framework Version

10.3.9

Link to Sentry event

No response

Reproduction Example/SDK Setup

Maybe I'm confused, but when using @SentryExeceptionCaptured() on an exception filter with a specific Catch condition, it doesn't report the error to sentry. It does work with a Catch-all all exception filter. e.g an exception filter an empty @Catch() decorator

If custom filters require manually catching the exception, maybe we can make this more apparent in the docs. The only thing it mentioned about custom exception filters is that if you want to catch 4.x.x errors you have to do it manually.

Custom Filter (Doesn't work)

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {

  @SentryExceptionCaptured()
  catch(
    exception: HttpException & BaseApiErrorResponse,
    host: ArgumentsHost
  ): void {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();

    response.status(500).json({error: "Some error"});
  }
}

Catch All Filter (Works)

@Catch()
export class AllExceptionFilter implements ExceptionFilter {

  @SentryExceptionCaptured()
  catch(
    exception: unknown & { getStatus?: () => number; message?: string },
    host: ArgumentsHost
  ): void {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();

    response.status(500).json({error: "testing error"});
  }
}

app.module.ts

@Module({
  imports: [
    SentryModule.forRoot(),
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_FILTER,
      useClass: AllExceptionFilter,
    },
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
    {
      provide: APP_FILTER,
      useClass: AxiosExceptionFilter,
    },
    {
      provide: APP_GUARD,
      useClass: RoleGuard,
    },
  ],
})
export class AppModule {}

Steps to Reproduce

1.) Create a global exception filter using @SentryExceptionCaptured()
2.) Create a targeted exception filter e.g @Catch(TestingError)
3.) Global filter catches exceptions, custom filter does not

Expected Result

I would imagine that @SentryExceptionCaptured() should catch the exception when specified on any catch function of an exception filter

Actual Result

Exceptions caught by specific exception filters are not reported

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPackage: nestjsIssues related to the Sentry Nestjs SDK

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions