Sentry Raven Module for Nest framework
This's a @sentry/minimal module for Nest.
$ npm i --save nest-raven
- 4.x Is for Next v6.x
- 3.x Is for Nest v5.x and introduces @sentry/minimal
- 2.x Is for Nest v5.x
- 1.x Is for Nest v4.x
- Client needs to be initialised by the user (outside of this module).
- Instead of
@UseInterceptors(RavenInterceptor())
you now have to do@UseInterceptors(new RavenInterceptor())
- When importing, you just specify module, there is no need for calling
forRoot()
anymore.
For Module to work you need to setup Sentry SDK yourself,
this should be done in your main.ts
file where you initialize the NestJS application.
app.module.ts
@Module({
imports: [
...
RavenModule,
]
})
export class ApplicationModule implements NestModule {
}
app.controller.ts
@UseInterceptors(new RavenInterceptor())
@Get('/some/route')
public async someRoute() {
...
}
With this setup, sentry will pick up all exceptions (even 400 types).
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):
app.module.ts
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
imports: [
RavenModule,
],
providers: [
{
provide: APP_INTERCEPTOR,
useValue: new RavenInterceptor(),
},
],
})
export class ApplicationModule {}
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.
app.controller.ts
@UseInterceptors(new RavenInterceptor({
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() {
...
}
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
app.controller.ts
@UseInterceptors(new RavenInterceptor({
tags: {
type: 'fileUpload',
},
level: 'warning',
}))
@Get('/some/route')
public async someRoute()
...
}
Note: Websockets ignore Global interceptors.
When using with websockets, you should provide context, as we cannot autmaticly detarmin if we are capturing http or websocket exception.
It will add ws_client
and ws_data
extras.
app.gateway.ts
@UseInterceptors(new RavenInterceptor({
context: 'Ws'
}))
@SubscribeMessage('message_name')
public someMessage(client, data: string): string {
...
}