BradleyFaught / nest-raven

Sentry Raven Module for Nest.js Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nest Logo

Sentry Raven Module for Nest framework

NPM Version Package License NPM Downloads Travis build Coveralls

Description

This's a @sentry/minimal module for Nest.

Installation

$ npm i --save nest-raven

Versions

  • 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

Breaking Changes in version 3.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.

Quick Start

Include Module

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 {
}

Using Interceptor

app.controller.ts

  @UseInterceptors(new RavenInterceptor())
  @Get('/some/route')
  public async someRoute() {
    ...
  }

With this setup, 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):

app.module.ts

import { APP_INTERCEPTOR } from '@nestjs/core';

@Module({
  imports: [
      RavenModule,
  ],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useValue: new RavenInterceptor(),
    },
  ],
})
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.

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() {
    ...
  }

Additional data

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()
    ...
  }

Websockets

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 {
    ...
  }

About

Sentry Raven Module for Nest.js Framework

License:MIT License


Languages

Language:TypeScript 100.0%