ntegral / nestjs-sentry

Provides an injectable sentry.io client to provide enterprise logging nestjs modules
ISC License
380 stars 98 forks source link

[bug]: Sentry does not capture exceptions #68

Open zhukevgeniy opened 2 years ago

zhukevgeniy commented 2 years ago

Input Code?

I'm trying to configure Sentry for nestjs application. I do not want to handle errors manually by using ::captureException. Instead, I want to send all my uncaught errors to the sentry.

@Module({
  imports: [
    SentryModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: function (configService: ConfigService) {
        return {
          dsn: configService.get<string>('SENTRY_DSN'),
          environment: process.env.NODE_ENV,
        };
      },
      inject: [ConfigService],
    }),
    ConfigModule.forRoot({ isGlobal: true }),
  ],
})
export class AppModule implements OnApplicationBootstrap {
  onApplicationBootstrap(): never {
    setTimeout(() => {
      throw new Error('Test error');
    }, 0);
  }
}

Current and expected behavior

Current: Command failed with exit code 1 with no exception sent to sentry Expected: Command failed with exit code 1 and exception sent to sentry

Environment

Node version — 16.13.1

Possible solution

We need to flush the sentry and wait before we terminate the process.

// lib/sentry.service.ts
  (Sentry.getCurrentHub().getClient<Client<Options>>() as Client<Options>).captureException(err);
  await Sentry.flush(); // <----- flush here
  process.exit(1);
xaphod commented 2 years ago

I don't think you're using the interceptor so nothing will get reported automatically. Look at the second message in this issue: https://github.com/ntegral/nestjs-sentry/issues/62