@@ -67,35 +67,47 @@ export { SentryTracingInterceptor };
67
67
*/
68
68
class SentryGlobalFilter extends BaseExceptionFilter {
69
69
public readonly __SENTRY_INTERNAL__ : boolean ;
70
+ private readonly _logger : Logger ;
70
71
71
72
public constructor ( applicationRef ?: HttpServer ) {
72
73
super ( applicationRef ) ;
73
74
this . __SENTRY_INTERNAL__ = true ;
75
+ this . _logger = new Logger ( 'ExceptionsHandler' ) ;
74
76
}
75
77
76
78
/**
77
79
* Catches exceptions and reports them to Sentry unless they are expected errors.
78
80
*/
79
81
public catch ( exception : unknown , host : ArgumentsHost ) : void {
80
- if ( isExpectedError ( exception ) ) {
81
- return super . catch ( exception , host ) ;
82
+ // The BaseExceptionFilter does not work well in GraphQL applications.
83
+ // By default, Nest GraphQL applications use the ExternalExceptionFilter, which just rethrows the error:
84
+ // https://github.com/nestjs/nest/blob/master/packages/core/exceptions/external-exception-filter.ts
85
+ if ( host . getType < 'graphql' > ( ) === 'graphql' ) {
86
+ // neither report nor log HttpExceptions
87
+ if ( exception instanceof HttpException ) {
88
+ throw exception ;
89
+ }
90
+
91
+ if ( exception instanceof Error ) {
92
+ this . _logger . error ( exception . message , exception . stack ) ;
93
+ }
94
+
95
+ captureException ( exception ) ;
96
+ throw exception ;
97
+ }
98
+
99
+ if ( ! isExpectedError ( exception ) ) {
100
+ captureException ( exception ) ;
82
101
}
83
102
84
- captureException ( exception ) ;
85
103
return super . catch ( exception , host ) ;
86
104
}
87
105
}
88
106
Catch ( ) ( SentryGlobalFilter ) ;
89
107
export { SentryGlobalFilter } ;
90
108
91
109
/**
92
- * Global filter to handle exceptions and report them to Sentry.
93
- *
94
- * The BaseExceptionFilter does not work well in GraphQL applications.
95
- * By default, Nest GraphQL applications use the ExternalExceptionFilter, which just rethrows the error:
96
- * https://github.com/nestjs/nest/blob/master/packages/core/exceptions/external-exception-filter.ts
97
- *
98
- * The ExternalExceptinFilter is not exported, so we reimplement this filter here.
110
+ * Global filter to handle exceptions in NestJS + GraphQL applications and report them to Sentry.
99
111
*/
100
112
class SentryGlobalGraphQLFilter {
101
113
private static readonly _logger = new Logger ( 'ExceptionsHandler' ) ;
@@ -129,29 +141,7 @@ export { SentryGlobalGraphQLFilter };
129
141
*
130
142
* This filter is a generic filter that can handle both HTTP and GraphQL exceptions.
131
143
*/
132
- class SentryGlobalGenericFilter extends SentryGlobalFilter {
133
- public readonly __SENTRY_INTERNAL__ : boolean ;
134
- private readonly _graphqlFilter : SentryGlobalGraphQLFilter ;
135
-
136
- public constructor ( applicationRef ?: HttpServer ) {
137
- super ( applicationRef ) ;
138
- this . __SENTRY_INTERNAL__ = true ;
139
- this . _graphqlFilter = new SentryGlobalGraphQLFilter ( ) ;
140
- }
141
-
142
- /**
143
- * Catches exceptions and forwards them to the according error filter.
144
- */
145
- public catch ( exception : unknown , host : ArgumentsHost ) : void {
146
- if ( host . getType < 'graphql' > ( ) === 'graphql' ) {
147
- return this . _graphqlFilter . catch ( exception , host ) ;
148
- }
149
-
150
- super . catch ( exception , host ) ;
151
- }
152
- }
153
- Catch ( ) ( SentryGlobalGenericFilter ) ;
154
- export { SentryGlobalGenericFilter } ;
144
+ export const SentryGlobalGenericFilter = SentryGlobalFilter ;
155
145
156
146
/**
157
147
* Service to set up Sentry performance tracing for Nest.js applications.
0 commit comments