Skip to content

Added Support for NestJS 9 and updated README.md #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules
node_modules
31 changes: 26 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { SentryModule } from '@ntegral/nestjs-sentry';
debug: true | false,
environment: 'dev' | 'production' | 'some_environment',
release: 'some_release', | null, // must create a release in sentry.io dashboard
logLevels: ['debug']
logLevels: ['debug'] //based on sentry.io loglevel //
}),
],
})
Expand Down Expand Up @@ -127,13 +127,12 @@ You can instuct use the SentryService to log non-error messages as breadcrumbs o
```typescript
import { Injectable } from '@nestjs/common';
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry';
import { Severity } from '@sentry/types';

@Injectable()
export class AppService {
constructor(@InjectSentry() private readonly client: SentryService) {
client.log('AppSevice Loaded','test', true); // creates log asBreadcrumb //
client.instance().addBreadcrumb({level: Severity.Debug, message: 'How to use native breadcrumb', data: { context: 'WhatEver'}})
client.instance().addBreadcrumb({level: 'debug' , message: 'How to use native breadcrumb', data: { context: 'WhatEver'}})
client.debug('AppService Debug', 'context');
}
getHello(): string {
Expand Down Expand Up @@ -168,6 +167,29 @@ import { GraphqlInterceptor } from '@ntegral/nestjs-sentry';
export class AppModule {}
```

Using the sentry interceptor globally
```typescript
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { SentryInterceptor } from '@ntegral/nestjs-sentry';

@Module({
....
providers: [
{
provide: APP_INTERCEPTOR,
useFactory: () => new SentryInterceptor({
filters: [{
type: HttpException,
filter: (exception: HttpException) => 500 > exception.getStatus() // Only report 500 errors
}]
}),
}
],
})
export class AppModule {}
```

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

@Module({
imports: [
Expand All @@ -188,7 +209,7 @@ import { LogLevel } from '@sentry/types';
debug: true | false,
environment: 'dev' | 'production' | 'some_environment',
release: 'some_release', | null, // must create a release in sentry.io dashboard
logLevels: ['debug']
logLevels: ['debug'] //based on sentry.io loglevel //
close: {
enabled: true,
// Time in milliseconds to forcefully quit the application
Expand Down
3 changes: 1 addition & 2 deletions lib/__tests__/sentry.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Test, TestingModule } from '@nestjs/testing';
import { LogLevel } from "@sentry/types";
import { SentryModuleOptions } from '../sentry.interfaces';
import { Injectable } from '@nestjs/common';
import { InjectSentry } from '../sentry.decorator';
Expand All @@ -12,7 +11,7 @@ describe('InjectS3', () => {
dsn: 'https://[email protected]/25956308132020',
debug: true,
environment: 'development',
logLevel: LogLevel.Debug,
logLevels: ['debug'],
}
let module: TestingModule;

Expand Down
3 changes: 1 addition & 2 deletions lib/__tests__/sentry.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Test } from '@nestjs/testing';

import { SentryModule } from '../sentry.module';
import { SentryModuleOptions, SentryOptionsFactory } from '../sentry.interfaces';
import { LogLevel } from '@sentry/types';
import { SentryService } from '../sentry.service';
import { SENTRY_TOKEN } from '../sentry.constants';
import { Module } from '@nestjs/common';
Expand All @@ -12,7 +11,7 @@ describe('SentryModule', () => {
dsn: 'https://[email protected]/25956308132020',
debug: true,
environment: 'development',
logLevel: LogLevel.Debug,
logLevels: ['debug'],
}

class TestService implements SentryOptionsFactory {
Expand Down
5 changes: 2 additions & 3 deletions lib/__tests__/sentry.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SentryModuleOptions, SentryOptionsFactory } from "../sentry.interfaces";
import { LogLevel } from "@sentry/types";
import { Test, TestingModule } from "@nestjs/testing";
import { SentryModule } from "../sentry.module";
import { SentryService } from "../sentry.service";
Expand All @@ -18,14 +17,14 @@ describe('SentryService', () => {
dsn: 'https://[email protected]/25956308132020',
debug: true,
environment: 'development',
logLevel: LogLevel.Debug,
logLevels: ['debug'],
};

let failureConfig: SentryModuleOptions = {
dsn: 'https://[email protected]/1512xxx',
debug: true,
environment: 'development',
logLevel: LogLevel.Debug,
logLevels: ['debug'],
};

class TestService implements SentryOptionsFactory {
Expand Down
9 changes: 5 additions & 4 deletions lib/sentry.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
CallHandler,
ExecutionContext,
HttpException,
Injectable,
NestInterceptor
} from '@nestjs/common';
Expand Down Expand Up @@ -33,17 +34,17 @@ export class SentryInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
// first param would be for events, second is for errors
return next.handle().pipe(
tap(null, (exception) => {
tap(null, (exception : HttpException) => {
if(this.shouldReport(exception)) {
this.client.instance().withScope((scope) => {
this.captureException(context, scope, exception);
return this.captureException(context, scope, exception);
})
}
})
);
}

protected captureException(context: ExecutionContext, scope: Scope, exception: any) {
protected captureException(context: ExecutionContext, scope: Scope, exception: HttpException) {
switch (context.getType<ContextType>()) {
case 'http':
return this.captureHttpException(
Expand All @@ -66,7 +67,7 @@ export class SentryInterceptor implements NestInterceptor {
}
}

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

scope.setExtra('req', data.request);
Expand Down
6 changes: 3 additions & 3 deletions lib/sentry.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModuleMetadata, Type } from "@nestjs/common/interfaces";
import { Integration, Options } from '@sentry/types';
import { Severity } from "@sentry/node";
import { ConsoleLoggerOptions } from "@nestjs/common";
import { ConsoleLoggerOptions, HttpException } from "@nestjs/common";
import { SeverityLevel } from "@sentry/node";

export interface SentryCloseOptions {
enabled: boolean;
Expand Down Expand Up @@ -41,7 +41,7 @@ export interface SentryInterceptorOptions {
tags?: { [key: string]: string };
extra?: { [key: string]: any };
fingerprint?: string[];
level?: Severity;
level?: SeverityLevel;

// https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/handlers.ts#L163
request?: boolean;
Expand Down
24 changes: 12 additions & 12 deletions lib/sentry.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Injectable, ConsoleLogger } from '@nestjs/common';
import { OnApplicationShutdown } from '@nestjs/common';
import { Options, Client } from '@sentry/types';
import { ClientOptions, Client } from '@sentry/types';
import * as Sentry from '@sentry/node';
import { SENTRY_MODULE_OPTIONS } from './sentry.constants';
import { SentryModuleOptions } from './sentry.interfaces';
Expand Down Expand Up @@ -31,8 +31,8 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
} else {
(
Sentry.getCurrentHub().getClient<
Client<Options>
>() as Client<Options>
Client<ClientOptions>
>() as Client<ClientOptions>
).captureException(err);
process.exit(1);
}
Expand All @@ -58,20 +58,20 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
asBreadcrumb ?
Sentry.addBreadcrumb({
message,
level: Sentry.Severity.Log,
level: 'log',
data: {
context
}
}) :
Sentry.captureMessage(message, Sentry.Severity.Log);
Sentry.captureMessage(message, 'log');
} catch (err) {}
}

error(message: string, trace?: string, context?: string) {
message = `${this.app} ${message}`;
try {
super.error(message, trace, context);
Sentry.captureMessage(message, Sentry.Severity.Error);
Sentry.captureMessage(message, 'error');
} catch (err) {}
}

Expand All @@ -82,12 +82,12 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
asBreadcrumb ?
Sentry.addBreadcrumb({
message,
level: Sentry.Severity.Warning,
level: 'warning',
data: {
context
}
}) :
Sentry.captureMessage(message, Sentry.Severity.Warning);
Sentry.captureMessage(message, 'warning');
} catch (err) {}
}

Expand All @@ -98,12 +98,12 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
asBreadcrumb ?
Sentry.addBreadcrumb({
message,
level: Sentry.Severity.Debug,
level: 'debug',
data: {
context
}
}) :
Sentry.captureMessage(message, Sentry.Severity.Debug);
Sentry.captureMessage(message, 'debug');
} catch (err) {}
}

Expand All @@ -114,12 +114,12 @@ export class SentryService extends ConsoleLogger implements OnApplicationShutdow
asBreadcrumb ?
Sentry.addBreadcrumb({
message,
level: Sentry.Severity.Info,
level: 'info',
data: {
context
}
}) :
Sentry.captureMessage(message, Sentry.Severity.Info);
Sentry.captureMessage(message, 'info');
} catch (err) {}
}

Expand Down
Loading