/
raven.interceptor.ts
94 lines (78 loc) · 3.06 KB
/
raven.interceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {
ExecutionContext, Injectable,
NestInterceptor,
CallHandler,
} from '@nestjs/common';
import { IRavenInterceptorOptions } from './raven.interfaces';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import * as Sentry from '@sentry/minimal'
import { Scope } from '@sentry/hub';
import { RpcArgumentsHost, WsArgumentsHost, HttpArgumentsHost } from '@nestjs/common/interfaces';
@Injectable()
export class RavenInterceptor implements NestInterceptor {
constructor(
private readonly options: IRavenInterceptorOptions = { context: 'Http' },
) {
}
intercept(
context: ExecutionContext,
next: CallHandler,
): Observable<any> {
const http = context.switchToHttp();
const ws = context.switchToWs();
const rpc = context.switchToRpc();
// Default to Http as conteext
if (!this.options.context) this.options.context = 'Http';
// first param would be for events, second is for errors
return next.handle()
.pipe(
tap(null, (exception) => {
if (this.shouldReport(exception)) {
Sentry.withScope(scope => {
// TODO: When https://github.com/nestjs/nest/issues/1581 gets implemented switch to that
switch(this.options.context){
case 'Http': return this.captureHttpException(scope, http, exception);
case 'Ws': return this.captureWsException(scope, ws, exception);
case 'Rpc': return this.captureRpcException(scope, rpc, exception);
}
});
}
})
);
}
private captureHttpException(scope: Scope, http: HttpArgumentsHost, exception): void {
scope.setExtra('req', http.getRequest());
scope.setExtra('res', http.getResponse());
if (http.getRequest() && http.getRequest().user) scope.setUser(http.getRequest().user);
this.captureException(scope, exception);
}
private captureRpcException(scope: Scope, rpc: RpcArgumentsHost, exception): void {
scope.setExtra('rpc_data', rpc.getData());
this.captureException(scope, exception)
}
private captureWsException(scope: Scope, ws: WsArgumentsHost, exception): void {
scope.setExtra('ws_client', ws.getClient());
scope.setExtra('ws_data', ws.getData());
this.captureException(scope, exception)
}
private captureException(scope: Scope, exception): void {
if (this.options.level) scope.setLevel(this.options.level);
if (this.options.fingerprint) scope.setFingerprint(this.options.fingerprint);
if (this.options.extra) for (const key in this.options.extra) {
if(this.options.extra.hasOwnProperty(key)) scope.setExtra(key, this.options.extra[key]);
}
for (const tag in this.options.tags) {
scope.setTag(tag, this.options.tags[tag])
}
Sentry.captureException(exception);
}
private shouldReport(exception: any): boolean {
if (!this.options.filters) return true;
// If all filters pass, then we do not report
return this.options.filters
.every(({type, filter}) => {
return !(exception instanceof type && (!filter || filter(exception)));
});
}
}