Git Product home page Git Product logo

Comments (11)

webberig avatar webberig commented on July 30, 2024 1

I suspect that the default unhandled exception handling isn't working since NestJS actually handles all exceptions by default ?

This is my solution:

Create an Interceptor:

@Injectable()
export class SentryReportingInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, call$) {
        return call$
            .pipe(catchError(error => {
                withScope(scope => {
                    scope.addEventProcessor(event => parseRequest(event, context.switchToHttp().getRequest()));
                    captureException(error);
                });
                throw error;
            }));
    }
}

And configure it globally in your appModule:

    providers: [
        {
            provide: APP_INTERCEPTOR,
            useClass: SentryReportingInterceptor,
        }
    ],

I think this library could use a construction like this to properly setup the request scope and handle NestJS exceptions.

from nestjs-sentry.

ntegral avatar ntegral commented on July 30, 2024

The SentryService extends the nestjs/logger. The configurations on the NestJs documentation site are implemented. Please refer to https://docs.nestjs.com/techniques/logger to see the different configuration options.

from nestjs-sentry.

ntegral avatar ntegral commented on July 30, 2024

Did you accomplish what you were looking to do?

from nestjs-sentry.

cesar3030 avatar cesar3030 commented on July 30, 2024

@ntegral Hi, I referred to the link you posted but I still have issue to have it working.
here is my config:

main.ts:

import { SentryService } from '@ntegral/nestjs-sentry';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useLogger(app.get(SentryService));  
  await app.listen(configService.get('PORT'));
}

app.module.ts:

import { Module } from '@nestjs/common';
import { SentryModule } from '@ntegral/nestjs-sentry';
import { LogLevel } from '@sentry/types';
...

@Module({
  imports: [
    SentryModule.forRootAsync({
      useFactory: async (configService: ConfigurationService) => ({
        dsn: configService.get('SENTRY_DSN'),
        debug: configService.get('SENTRY_DEBUG') == 'true',
        environment: configService.get('SENTRY_ENVIRONMENT'),
        //release: 'some_release', | null, // must create a release in sentry.io dashboard
        logLevel: LogLevel.Debug //based on sentry.io loglevel //
      }),
      inject: [ConfigurationService],
    }),
    ...
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

When I look at my Sentry dashboard, I only see some info/warning message generated during app startup but none of the exceptions or JS Errors like null.push(''test') from my graphql resolver are captured.
Screen Shot 2019-10-09 at 7 02 29 PM

JS Error:

  @Mutation(returns => User)
  @UseInterceptors(PictureInterceptor)
  async login(@Args() loginArgs: LoginArgs): Promise<User> {
    const t = null;
    t.push('l'); // <- throws an error
    return this.authService.login(loginArgs)
  }

Throw an error:

  @Mutation(returns => User)
  @UseInterceptors(PictureInterceptor)
  async login(@Args() loginArgs: LoginArgs): Promise<User> {
    throw new Error('Sentry test');
    return this.authService.login(loginArgs)
  }

Do you have any idea why? is It because i'm using GraphQL?

from nestjs-sentry.

ntegral avatar ntegral commented on July 30, 2024

I'll look into your issue.

This definitely works.. It allows you to send message to sentry.io based on your above example.
put the code inside a try/catch statement..
constructor(@InjectSentry() private readonly client: SentryService) {
client.error('test message');
}

from nestjs-sentry.

cesar3030 avatar cesar3030 commented on July 30, 2024

it works as expected when I inject the sentryService into the resolver constructor and I call client.error('test message'); but if I throw an Error, it does not work.

When I call client.error('test message'); from the GraphQL resolver, The event is logged twice:
Screen Shot 2019-10-11 at 3 48 31 PM

To avoid the event to be logged twice I need to instantiate the Nest App like this:

const app = await NestFactory.create(AppModule, {
   logger: false,
});

It is GraphQL related because when I throw an Error from a REST Controller, it is sent to Sentry. Only one event is sent to sentry even if logger is not disabled.

When an exception is thrown from a controller, the strack trace is not sent to Sentry:

@Get('/')
  public async get() {
    const t = null;
    t.push('t');
  }

Sentry:
Screen Shot 2019-10-11 at 4 20 05 PM

Tell me if you want me to create different issues 😄

Thank you very much for your help.

I'm going to investigate why Error thrown in GraphQL resolver are not even captured my nest internal logger (nothing is printed to the console).

from nestjs-sentry.

cesar3030 avatar cesar3030 commented on July 30, 2024

@ntegral any updates on this?

from nestjs-sentry.

cesar3030 avatar cesar3030 commented on July 30, 2024

I created an issue in nestjs/graphql repo: nestjs/graphql#449 to help understanding what's going on

from nestjs-sentry.

cesar3030 avatar cesar3030 commented on July 30, 2024

Updating to the last @nestjs/core version (6.9.0) fixed the issue.
Command to do it with nestcli: nest u -f

from nestjs-sentry.

ntegral avatar ntegral commented on July 30, 2024

from nestjs-sentry.

cesar3030 avatar cesar3030 commented on July 30, 2024

My pleasure, Thanks for your work!

from nestjs-sentry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.