nest-sentry v0.3.0
nest-sentry
nest-sentry
avails a module, which injects a service that wraps the sentry library into your
dependency injection workflow as well as providing an interceptor for more better error handling situations.
Installation
npm i --save nest-sentry
yarn add nest-sentry
Getting Started
The simplest way to use nest-sentry
is to use SentryModule.forRoot
import { Module } from '@nestjs-common';
import { SentryModule } from 'nest-sentry';
@Module({
imports: [
SentryModule.forRoot({
dsn: 'sentry_io_dsn',
debug: true | false,
environment: 'production' | 'some_environment',
release: 'some_release', | null, // must create a release in sentry.io dashboard
logLevel: LogLevel.Debug
}),
],
})
export class AppModule {}
The async way nest-sentry
is to use SentryModule.forRootAsync
import { Module } from '@nestjs-common';
import { SentryModule } from 'nest-sentry';
import { ConfigModule } from 'nestjs-config';
import { ConfigService } from 'nestjs-config';
@Module({
imports: [
SentryModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (cfg:ConfigService) => ({
dsn: cfg.get('SENTRY_DSN'),
debug: true | false,
environment: 'production' | 'some_environment',
release: 'some_release', | null, // must create a release in sentry.io dashboard
logLevel: LogLevel.Debug // based on sentry.io loglevel
}),
inject: [ConfigService],
})
]
})
export class AppModule {}
You can then inject the Sentry client into any of your injectables by using a custom decorator
import { Injectable } from '@nestjs/common';
import { InjectSentry, SentryService } from 'nest-sentry';
@Injectable()
export class AppService {
public constructor(@InjectSentry() private readonly sentry: SentryService) {
sentry.captureMessage(message, Sentry.Severity.Log);
sentry.captureException(exception);
}
handler(){
this.sentry.log('Handler fired');
}
}
Interceptor & or Filter
You can also use the sentry interceptor, in your application / microservice, though this may require you to init
the Sentry SDK yourself in the main.ts
file
Sentry.init({dsn: 'YOUR_SENTRY_KEY'});
@UseInterceptors(new SentryInterceptor())
@Get('/sample/route')
public async sampleRoute() {
...
}
With this setup though, sentry will pick up all exceptions (even 400 types).
Global
If you want to set up interceptor as global, you have to follow Nest instructions here. Something like this. This only works for Controllers not for Gateways (limitation by NestJS):
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useValue: new SentryInterceptor(),
},
],
})
export class ApplicationModule {}
Filters
Sometimes we don't want to catch all exceptions but only 500 or those that we didn't handle properly. For that we can add filters on interceptor to filter out good exceptions.
@UseInterceptors(new SentryInterceptor({
filters: [
// Filter exceptions of type HttpException. Ignore those that
// have status code of less than 500
{ type: HttpException, filter: (exception: HttpException) => 500 > exception.getStatus() }
],
}))
@Get('/some/route')
public async someRoute() {
...
}
Additional data
The interceptor automatically adds req
and req.user
(as user) to additional data.
Other additional data can be added for each interceptor.
- tags
- extra
- fingerprint
- level
@UseInterceptors(new SentryInterceptor({
tags: {
type: 'fileUpload',
},
level: 'warning',
}))
@Get('/some/route')
public async someRoute()
...
}
Credits
Based off of ntegral's, @ntegral/nestjs-sentry & mentos1386's nest-raven