Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

nest-raven

5.0.0 • Public • Published

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

  • 5.x Upgrades @sentry/minimal to v5.x from v4.x. Addes @sentry/node to dependencies.
  • 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, datastring)string {
    ...
  }

GraphQL

Warning: This is an ALPHA level of support. There are bugs, not intended for production.

When using with graphql, you should provide context, as we cannot autmaticly detarmin if we are capturing http or graphql exception.

It will add fieldname and args extras.

app.gateway.ts

  @Mutation()
  @UseInterceptors(new RavenInterceptor({
    context: 'GraphQL'
  }))
  async upvotePost(@Args('postId') postIdnumber) {
    ...
  }

Install

npm i nest-raven

DownloadsWeekly Downloads

7,505

Version

5.0.0

License

MIT

Unpacked Size

14.8 kB

Total Files

11

Last publish

Collaborators

  • avatar