Skip to content

Commit 2554847

Browse files
committed
Added Support for NestJS 9 and updated README.md
1 parent 4f7ad4e commit 2554847

10 files changed

+6162
-2128
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
dist/*
3+
coverage/*

README.MD

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ The simplest way to use `@ntegral/nestjs-sentry` is to use `SentryModule.forRoot
5151
```typescript
5252
import { Module } from '@nestjs-common';
5353
import { SentryModule } from '@ntegral/nestjs-sentry';
54-
import { LogLevel } from '@sentry/types';
5554

5655
@Module({
5756
imports: [
@@ -60,7 +59,7 @@ import { LogLevel } from '@sentry/types';
6059
debug: true | false,
6160
environment: 'dev' | 'production' | 'some_environment',
6261
release: 'some_release', | null, // must create a release in sentry.io dashboard
63-
logLevel: LogLevel.Debug //based on sentry.io loglevel //
62+
logLevels: ['debug'] //based on sentry.io loglevel //
6463
}),
6564
],
6665
})
@@ -84,7 +83,7 @@ import { ConfigService } from '@ntegral/nestjs-config';
8483
debug: true | false,
8584
environment: 'dev' | 'production' | 'some_environment',
8685
release: 'some_release', | null, // must create a release in sentry.io dashboard
87-
logLevel: LogLevel.Debug //based on sentry.io loglevel //
86+
logLevels: ['debug'] //based on sentry.io loglevel //
8887
}),
8988
inject: [ConfigService],
9089
})
@@ -128,13 +127,12 @@ You can instuct use the SentryService to log non-error messages as breadcrumbs o
128127
```typescript
129128
import { Injectable } from '@nestjs/common';
130129
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry';
131-
import { Severity } from '@sentry/types';
132130

133131
@Injectable()
134132
export class AppService {
135133
constructor(@InjectSentry() private readonly client: SentryService) {
136134
client.log('AppSevice Loaded','test', true); // creates log asBreadcrumb //
137-
client.instance().addBreadcrumb({level: Severity.Debug, message: 'How to use native breadcrumb', data: { context: 'WhatEver'}})
135+
client.instance().addBreadcrumb({level: 'debug' , message: 'How to use native breadcrumb', data: { context: 'WhatEver'}})
138136
client.debug('AppService Debug', 'context');
139137
}
140138
getHello(): string {
@@ -169,6 +167,29 @@ import { GraphqlInterceptor } from '@ntegral/nestjs-sentry';
169167
export class AppModule {}
170168
```
171169

170+
Using the sentry interceptor globally
171+
```typescript
172+
import { Module } from '@nestjs/common';
173+
import { APP_INTERCEPTOR } from '@nestjs/core';
174+
import { SentryInterceptor } from '@ntegral/nestjs-sentry';
175+
176+
@Module({
177+
....
178+
providers: [
179+
{
180+
provide: APP_INTERCEPTOR,
181+
useFactory: () => new SentryInterceptor({
182+
filters: [{
183+
type: HttpException,
184+
filter: (exception: HttpException) => 500 > exception.getStatus() // Only report 500 errors
185+
}]
186+
}),
187+
}
188+
],
189+
})
190+
export class AppModule {}
191+
```
192+
172193
## Flushing sentry
173194
Sentry does not flush all the errors by itself, it does it in background so that it doesn't block the main thread. If
174195
you kill the nestjs app forcefully some exceptions don't have to be flushed and logged successfully.
@@ -180,7 +201,6 @@ for closing (flushing) to work.
180201
```typescript
181202
import { Module } from '@nestjs-common';
182203
import { SentryModule } from '@ntegral/nestjs-sentry';
183-
import { LogLevel } from '@sentry/types';
184204

185205
@Module({
186206
imports: [
@@ -189,7 +209,7 @@ import { LogLevel } from '@sentry/types';
189209
debug: true | false,
190210
environment: 'dev' | 'production' | 'some_environment',
191211
release: 'some_release', | null, // must create a release in sentry.io dashboard
192-
logLevel: LogLevel.Debug //based on sentry.io loglevel //
212+
logLevels: ['debug'] //based on sentry.io loglevel //
193213
close: {
194214
enabled: true,
195215
// Time in milliseconds to forcefully quit the application

lib/__tests__/sentry.decorator.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { LogLevel } from "@sentry/types";
32
import { SentryModuleOptions } from '../sentry.interfaces';
43
import { Injectable } from '@nestjs/common';
54
import { InjectSentry } from '../sentry.decorator';
@@ -12,7 +11,7 @@ describe('InjectS3', () => {
1211
dsn: 'https://[email protected]/25956308132020',
1312
debug: true,
1413
environment: 'development',
15-
logLevel: LogLevel.Debug,
14+
logLevels: ['debug'],
1615
}
1716
let module: TestingModule;
1817

lib/__tests__/sentry.module.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Test } from '@nestjs/testing';
22

33
import { SentryModule } from '../sentry.module';
44
import { SentryModuleOptions, SentryOptionsFactory } from '../sentry.interfaces';
5-
import { LogLevel } from '@sentry/types';
65
import { SentryService } from '../sentry.service';
76
import { SENTRY_TOKEN } from '../sentry.constants';
87
import { Module } from '@nestjs/common';
@@ -12,7 +11,7 @@ describe('SentryModule', () => {
1211
dsn: 'https://[email protected]/25956308132020',
1312
debug: true,
1413
environment: 'development',
15-
logLevel: LogLevel.Debug,
14+
logLevels: ['debug'],
1615
}
1716

1817
class TestService implements SentryOptionsFactory {

lib/__tests__/sentry.service.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { SentryModuleOptions, SentryOptionsFactory } from "../sentry.interfaces";
2-
import { LogLevel } from "@sentry/types";
32
import { Test, TestingModule } from "@nestjs/testing";
43
import { SentryModule } from "../sentry.module";
54
import { SentryService } from "../sentry.service";
@@ -18,14 +17,14 @@ describe('SentryService', () => {
1817
dsn: 'https://[email protected]/25956308132020',
1918
debug: true,
2019
environment: 'development',
21-
logLevel: LogLevel.Debug,
20+
logLevels: ['debug'],
2221
};
2322

2423
let failureConfig: SentryModuleOptions = {
2524
dsn: 'https://[email protected]/1512xxx',
2625
debug: true,
2726
environment: 'development',
28-
logLevel: LogLevel.Debug,
27+
logLevels: ['debug'],
2928
};
3029

3130
class TestService implements SentryOptionsFactory {

lib/sentry.interceptor.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
CallHandler,
44
ExecutionContext,
5+
HttpException,
56
Injectable,
67
NestInterceptor
78
} from '@nestjs/common';
@@ -33,17 +34,17 @@ export class SentryInterceptor implements NestInterceptor {
3334
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
3435
// first param would be for events, second is for errors
3536
return next.handle().pipe(
36-
tap(null, (exception) => {
37+
tap(null, (exception : HttpException) => {
3738
if(this.shouldReport(exception)) {
3839
this.client.instance().withScope((scope) => {
39-
this.captureException(context, scope, exception);
40+
return this.captureException(context, scope, exception);
4041
})
4142
}
4243
})
4344
);
4445
}
4546

46-
protected captureException(context: ExecutionContext, scope: Scope, exception: any) {
47+
protected captureException(context: ExecutionContext, scope: Scope, exception: HttpException) {
4748
switch (context.getType<ContextType>()) {
4849
case 'http':
4950
return this.captureHttpException(
@@ -66,7 +67,7 @@ export class SentryInterceptor implements NestInterceptor {
6667
}
6768
}
6869

69-
private captureHttpException(scope: Scope, http: HttpArgumentsHost, exception: any): void {
70+
private captureHttpException(scope: Scope, http: HttpArgumentsHost, exception: HttpException): void {
7071
const data = Handlers.parseRequest(<any>{},http.getRequest(), this.options);
7172

7273
scope.setExtra('req', data.request);

lib/sentry.interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ModuleMetadata, Type } from "@nestjs/common/interfaces";
22
import { Integration, Options } from '@sentry/types';
3-
import { Severity } from "@sentry/node";
4-
import { ConsoleLoggerOptions } from "@nestjs/common";
3+
import { ConsoleLoggerOptions, HttpException } from "@nestjs/common";
4+
import { SeverityLevel } from "@sentry/node";
55

66
export interface SentryCloseOptions {
77
enabled: boolean;
@@ -41,7 +41,7 @@ export interface SentryInterceptorOptions {
4141
tags?: { [key: string]: string };
4242
extra?: { [key: string]: any };
4343
fingerprint?: string[];
44-
level?: Severity;
44+
level?: SeverityLevel;
4545

4646
// https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/handlers.ts#L163
4747
request?: boolean;

lib/sentry.service.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Inject, Injectable, ConsoleLogger } from '@nestjs/common';
22
import { OnApplicationShutdown } from '@nestjs/common';
3-
import { Options, Client } from '@sentry/types';
3+
import { ClientOptions, Client } from '@sentry/types';
44
import * as Sentry from '@sentry/node';
55
import { SENTRY_MODULE_OPTIONS } from './sentry.constants';
66
import { SentryModuleOptions } from './sentry.interfaces';
@@ -31,8 +31,8 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
3131
} else {
3232
(
3333
Sentry.getCurrentHub().getClient<
34-
Client<Options>
35-
>() as Client<Options>
34+
Client<ClientOptions>
35+
>() as Client<ClientOptions>
3636
).captureException(err);
3737
process.exit(1);
3838
}
@@ -58,20 +58,20 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
5858
asBreadcrumb ?
5959
Sentry.addBreadcrumb({
6060
message,
61-
level: Sentry.Severity.Log,
61+
level: 'log',
6262
data: {
6363
context
6464
}
6565
}) :
66-
Sentry.captureMessage(message, Sentry.Severity.Log);
66+
Sentry.captureMessage(message, 'log');
6767
} catch (err) {}
6868
}
6969

7070
error(message: string, trace?: string, context?: string) {
7171
message = `${this.app} ${message}`;
7272
try {
7373
super.error(message, trace, context);
74-
Sentry.captureMessage(message, Sentry.Severity.Error);
74+
Sentry.captureMessage(message, 'error');
7575
} catch (err) {}
7676
}
7777

@@ -82,12 +82,12 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
8282
asBreadcrumb ?
8383
Sentry.addBreadcrumb({
8484
message,
85-
level: Sentry.Severity.Warning,
85+
level: 'warning',
8686
data: {
8787
context
8888
}
8989
}) :
90-
Sentry.captureMessage(message, Sentry.Severity.Warning);
90+
Sentry.captureMessage(message, 'warning');
9191
} catch (err) {}
9292
}
9393

@@ -98,12 +98,12 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
9898
asBreadcrumb ?
9999
Sentry.addBreadcrumb({
100100
message,
101-
level: Sentry.Severity.Debug,
101+
level: 'debug',
102102
data: {
103103
context
104104
}
105105
}) :
106-
Sentry.captureMessage(message, Sentry.Severity.Debug);
106+
Sentry.captureMessage(message, 'debug');
107107
} catch (err) {}
108108
}
109109

@@ -114,12 +114,12 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
114114
asBreadcrumb ?
115115
Sentry.addBreadcrumb({
116116
message,
117-
level: Sentry.Severity.Info,
117+
level: 'info',
118118
data: {
119119
context
120120
}
121121
}) :
122-
Sentry.captureMessage(message, Sentry.Severity.Info);
122+
Sentry.captureMessage(message, 'info');
123123
} catch (err) {}
124124
}
125125

0 commit comments

Comments
 (0)