Skip to content

Commit 72751da

Browse files
authored
feat(nestjs): Handle GraphQL contexts in SentryGlobalFilter (#14320)
1 parent d41a04d commit 72751da

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

packages/nestjs/src/setup.ts

+23-33
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,47 @@ export { SentryTracingInterceptor };
6767
*/
6868
class SentryGlobalFilter extends BaseExceptionFilter {
6969
public readonly __SENTRY_INTERNAL__: boolean;
70+
private readonly _logger: Logger;
7071

7172
public constructor(applicationRef?: HttpServer) {
7273
super(applicationRef);
7374
this.__SENTRY_INTERNAL__ = true;
75+
this._logger = new Logger('ExceptionsHandler');
7476
}
7577

7678
/**
7779
* Catches exceptions and reports them to Sentry unless they are expected errors.
7880
*/
7981
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);
82101
}
83102

84-
captureException(exception);
85103
return super.catch(exception, host);
86104
}
87105
}
88106
Catch()(SentryGlobalFilter);
89107
export { SentryGlobalFilter };
90108

91109
/**
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.
99111
*/
100112
class SentryGlobalGraphQLFilter {
101113
private static readonly _logger = new Logger('ExceptionsHandler');
@@ -129,29 +141,7 @@ export { SentryGlobalGraphQLFilter };
129141
*
130142
* This filter is a generic filter that can handle both HTTP and GraphQL exceptions.
131143
*/
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;
155145

156146
/**
157147
* Service to set up Sentry performance tracing for Nest.js applications.

0 commit comments

Comments
 (0)