10.0.1 • Published 5 months ago

nest-raven v10.0.1

Weekly downloads
16,717
License
MIT
Repository
github
Last release
5 months ago

Description

This is a sentry module for Nest.

This package is no longer based on deprecated raven module, but rather on new stable @sentry/node module.

Installation

$ npm i --save nest-raven

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

Transformers

It may be useful to add some extra data to the Sentry's context before sending the payload. Adding some request-related properties for instance. To achieve this we can add scope transformers on interceptor to injecte some data dynamically.

app.controller.ts

  @UseInterceptors(new RavenInterceptor({
    transformers: [
        // Add an extra property to Sentry's scope
        (scope: Scope, context: ExecutionContext) => {
          const req = context.switchToHttp().getRequest<Request>();
          scope.addExtra('important query', req.query.important_query)
          scope.addExtra('important key', 'useful value');
        }
    ],
  }))
  @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

import { Severity } from '@sentry/node';

  @UseInterceptors(new RavenInterceptor({
    tags: {
      type: 'fileUpload',
    },
    level: Severity.Warning,
  }))
  @Get('/some/route')
  public async someRoute()
    ...
  }

Websockets

Note: Websockets ignore Global interceptors.

It will add ws_client and ws_data extras.

app.gateway.ts

  @UseInterceptors(new RavenInterceptor())
  @SubscribeMessage('message_name')
  public someMessage(client, data: string): string {
    ...
  }

GraphQL

It will add fieldName and args extras.

app.gateway.ts

  @Mutation()
  @UseInterceptors(new RavenInterceptor())
  async upvotePost(@Args('postId') postId: number) {
    ...
  }
10.0.1

5 months ago

10.0.0

8 months ago

9.2.0

1 year ago

9.1.0

1 year ago

9.0.0

2 years ago

8.2.0

2 years ago

8.1.0

2 years ago

8.0.0

3 years ago

7.2.0

3 years ago

7.1.0

3 years ago

7.0.0

4 years ago

6.0.0-alpha.2

4 years ago

6.0.0-alpha.1

4 years ago

6.0.0

4 years ago

5.0.0

5 years ago

4.0.0

5 years ago

3.0.0

5 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago