Skip to content

Commit f80c538

Browse files
authored
Merge pull request #93 from xiifain:master
Added Support for NestJS 9 and updated README.md
2 parents 4552f0c + 4e742e5 commit f80c538

10 files changed

+6312
-2214
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules
1+
node_modules

README.MD

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import { SentryModule } from '@ntegral/nestjs-sentry';
5959
debug: true | false,
6060
environment: 'dev' | 'production' | 'some_environment',
6161
release: 'some_release', | null, // must create a release in sentry.io dashboard
62-
logLevels: ['debug']
62+
logLevels: ['debug'] //based on sentry.io loglevel //
6363
}),
6464
],
6565
})
@@ -127,13 +127,12 @@ You can instuct use the SentryService to log non-error messages as breadcrumbs o
127127
```typescript
128128
import { Injectable } from '@nestjs/common';
129129
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry';
130-
import { Severity } from '@sentry/types';
131130

132131
@Injectable()
133132
export class AppService {
134133
constructor(@InjectSentry() private readonly client: SentryService) {
135134
client.log('AppSevice Loaded','test', true); // creates log asBreadcrumb //
136-
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'}})
137136
client.debug('AppService Debug', 'context');
138137
}
139138
getHello(): string {
@@ -168,6 +167,29 @@ import { GraphqlInterceptor } from '@ntegral/nestjs-sentry';
168167
export class AppModule {}
169168
```
170169

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+
171193
## Flushing sentry
172194
Sentry does not flush all the errors by itself, it does it in background so that it doesn't block the main thread. If
173195
you kill the nestjs app forcefully some exceptions have to be flushed and logged successfully.
@@ -179,7 +201,6 @@ for closing (flushing) to work.
179201
```typescript
180202
import { Module } from '@nestjs-common';
181203
import { SentryModule } from '@ntegral/nestjs-sentry';
182-
import { LogLevel } from '@sentry/types';
183204

184205
@Module({
185206
imports: [
@@ -188,7 +209,7 @@ import { LogLevel } from '@sentry/types';
188209
debug: true | false,
189210
environment: 'dev' | 'production' | 'some_environment',
190211
release: 'some_release', | null, // must create a release in sentry.io dashboard
191-
logLevels: ['debug']
212+
logLevels: ['debug'] //based on sentry.io loglevel //
192213
close: {
193214
enabled: true,
194215
// 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)