Skip to content

Commit

Permalink
feat: switched from raven to @sentry/minimal
Browse files Browse the repository at this point in the history
  • Loading branch information
mentos1386 committed Jan 3, 2019
1 parent 7776a7b commit ec218ff
Show file tree
Hide file tree
Showing 16 changed files with 877 additions and 5,166 deletions.
5 changes: 1 addition & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './raven.constants';
export * from './raven.interceptor.abstract';
export * from './raven.interceptor.mixin';
export * from './raven.interceptor'
export * from './raven.interfaces';
export * from './raven.module';
export * from './raven.providers';
2 changes: 0 additions & 2 deletions lib/raven.constants.ts

This file was deleted.

12 changes: 0 additions & 12 deletions lib/raven.interceptor.mixin.ts

This file was deleted.

32 changes: 15 additions & 17 deletions lib/raven.interceptor.abstract.ts → lib/raven.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import {
ExecutionContext, Inject, Injectable,
ExecutionContext, Injectable,
NestInterceptor,
} from '@nestjs/common';
import { RAVEN_SENTRY_PROVIDER } from './raven.constants';
import { IRavenInterceptorOptions } from './raven.interfaces';
import * as Raven from 'raven';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import * as Sentry from '@sentry/minimal'

@Injectable()
export abstract class AbstractRavenInterceptor implements NestInterceptor {

protected abstract readonly options: IRavenInterceptorOptions = {};
export class RavenInterceptor implements NestInterceptor {

constructor(
@Inject(RAVEN_SENTRY_PROVIDER) private ravenClient: Raven.Client,
private readonly options: IRavenInterceptorOptions = {},
) {
}

Expand All @@ -29,16 +26,17 @@ export abstract class AbstractRavenInterceptor implements NestInterceptor {
return call$.pipe(
tap(null, (exception) => {
if (this.shouldReport(exception)) {
this.ravenClient.captureException(
exception as any,
{
req: httpRequest,
user: userData,
tags: this.options.tags,
fingerprint: this.options.fingerprint,
level: this.options.level,
extra: this.options.extra,
});
Sentry.withScope(scope => {
scope.setExtra('req', httpRequest);
if (userData) scope.setUser(userData)
if (this.options.level) scope.setLevel(this.options.level);
if (this.options.fingerprint) scope.setFingerprint(this.options.fingerprint);
for (const tag in this.options.tags) {
scope.setTag(tag, this.options.tags[tag])
}

Sentry.captureException(exception);
});
}
})
);
Expand Down
9 changes: 2 additions & 7 deletions lib/raven.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import * as Raven from 'raven';

export interface IRavenConfig {
dsn: string;
options: Raven.ConstructorOptions;
}
import * as Sentry from '@sentry/types';

export interface IRavenFilterFunction {
(exception: any): boolean;
Expand All @@ -19,5 +14,5 @@ export interface IRavenInterceptorOptions {
tags?: { [key: string]: string };
extra?: { [key: string]: any };
fingerprint?: string[];
level?: string;
level?: Sentry.Severity;
}
31 changes: 2 additions & 29 deletions lib/raven.module.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
import { DynamicModule, Global, Module } from '@nestjs/common';
import { ravenSentryProviders } from './raven.providers';
import * as Raven from 'raven';
import { RAVEN_SENTRY_CONFIG } from './raven.constants';
import { Global, Module } from '@nestjs/common';

@Global()
@Module({
providers: [
...ravenSentryProviders,
],
exports: [
...ravenSentryProviders,
],
})
@Module({})
export class RavenModule {
static forRoot(dsn?: string, options?: Raven.ConstructorOptions): DynamicModule {
return {
module: RavenModule,
providers: [
{
provide: RAVEN_SENTRY_CONFIG,
useValue: { dsn, options },
},
],
exports: [
{
provide: RAVEN_SENTRY_CONFIG,
useValue: { dsn, options },
},
],
};
}
}
13 changes: 0 additions & 13 deletions lib/raven.providers.ts

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nest-raven",
"version": "2.1.0",
"version": "3.0.0",
"description": "Sentry Raven Module for Nest Framework",
"directories": {
"lib": "lib"
Expand Down Expand Up @@ -28,23 +28,23 @@
},
"homepage": "https://github.com/mentos1386/nest-raven#readme",
"dependencies": {
"raven": "^2.4.1"
"@sentry/minimal": "^4.4.1"
},
"devDependencies": {
"@nestjs/common": "^5.0.1",
"@nestjs/core": "^5.0.1",
"@nestjs/testing": "^5.0.1",
"@types/jest": "^23.0.0",
"@types/node": "^7.0.41",
"@types/raven": "^2.5.1",
"@sentry/hub": "4.4.1",
"coveralls": "^3.0.2",
"jest": "^23.1.0",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"supertest": "^3.1.0",
"ts-jest": "^22.4.6",
"ts-node": "^3.3.0",
"typescript": "^2.4.2"
"typescript": "^2.8.2"
},
"jest": {
"transform": {
Expand Down
34 changes: 18 additions & 16 deletions test/global.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { RAVEN_SENTRY_PROVIDER } from '../lib';
import { INestApplication } from '@nestjs/common';
import { CaptureOptions } from 'raven';
import { GlobalModule } from './global.module';
import { getCurrentHub } from '@sentry/hub';

declare var global: any;

describe('Global', () => {
let app: INestApplication;
let ravenData: {
error: Error,
options: CaptureOptions,
};
let ravenSentry = {
captureException: (error: Error, options?: CaptureOptions) => ravenData = { error, options },
const client = {
captureException: jest.fn(async () => Promise.resolve()),
};

beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [GlobalModule],
})
.overrideProvider(RAVEN_SENTRY_PROVIDER)
.useValue(ravenSentry)
.compile();

app = module.createNestApplication();
await app.init();
});

beforeEach(() => ravenData = null);
beforeEach(() => {
global.__SENTRY__ = {
hub: undefined,
};
});

it(`/GET error`, async () => {
await request(app.getHttpServer())
.get('/error')
.expect(500);
getCurrentHub().withScope(async () => {
getCurrentHub().bindClient(client);

expect(ravenData).not.toBeNull();
expect(ravenData.error).toBeInstanceOf(Error);
await request(app.getHttpServer())
.get('/error')
.expect(500);

expect(client.captureException.mock.calls[0][0]).toBeInstanceOf(Error);
});
});

afterAll(async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/global.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { APP_INTERCEPTOR } from '@nestjs/core';

@Module({
imports: [
RavenModule.forRoot('https://your:sdn@sentry.io/290747'),
RavenModule,
],
controllers: [
GlobalController,
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: RavenInterceptor(),
useValue: new RavenInterceptor(),
},
]
})
Expand Down
17 changes: 9 additions & 8 deletions test/hello.controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { Controller, Get, HttpException, HttpStatus, UseInterceptors } from '@nestjs/common';
import { RavenInterceptor } from '../lib';
import * as Sentry from '@sentry/types';

@Controller('')
export class HelloController {

@Get('works')
@UseInterceptors(RavenInterceptor())
@UseInterceptors(new RavenInterceptor())
works() {
return 'Works';
}

@Get('intercepted')
@UseInterceptors(RavenInterceptor())
@UseInterceptors(new RavenInterceptor())
intercepted() {
throw new Error('Something bad happened');
}

@Get('filter')
@UseInterceptors(RavenInterceptor({
@UseInterceptors(new RavenInterceptor({
filters: [
// Filter exceptions of type HttpException. Ignore those that
// have status code of less than 500
Expand All @@ -29,32 +30,32 @@ export class HelloController {
}

@Get('tags')
@UseInterceptors(RavenInterceptor({
@UseInterceptors(new RavenInterceptor({
tags: { 'A': 'AAA', 'B': 'BBB' },
}))
tags() {
throw new Error('Something bad happened');
}

@Get('extra')
@UseInterceptors(RavenInterceptor({
@UseInterceptors(new RavenInterceptor({
extra: { 'A': 'AAA', 'B': 'BBB' },
}))
extra() {
throw new Error('Something bad happened');
}

@Get('fingerprint')
@UseInterceptors(RavenInterceptor({
@UseInterceptors(new RavenInterceptor({
fingerprint: ['A', 'B'],
}))
fingerprint() {
throw new Error('Something bad happened');
}

@Get('level')
@UseInterceptors(RavenInterceptor({
level: 'CRAZY',
@UseInterceptors(new RavenInterceptor({
level: Sentry.Severity.Critical,
}))
level() {
throw new Error('Something bad happened');
Expand Down
Loading

0 comments on commit ec218ff

Please sign in to comment.