diff --git a/packages/nestjs/src/setup.ts b/packages/nestjs/src/setup.ts index a18f95417f11..0d7ccdb2c4dc 100644 --- a/packages/nestjs/src/setup.ts +++ b/packages/nestjs/src/setup.ts @@ -67,21 +67,39 @@ export { SentryTracingInterceptor }; */ class SentryGlobalFilter extends BaseExceptionFilter { public readonly __SENTRY_INTERNAL__: boolean; + private readonly _logger: Logger; public constructor(applicationRef?: HttpServer) { super(applicationRef); this.__SENTRY_INTERNAL__ = true; + this._logger = new Logger('ExceptionsHandler'); } /** * Catches exceptions and reports them to Sentry unless they are expected errors. */ public catch(exception: unknown, host: ArgumentsHost): void { - if (isExpectedError(exception)) { - return super.catch(exception, host); + // The BaseExceptionFilter does not work well in GraphQL applications. + // By default, Nest GraphQL applications use the ExternalExceptionFilter, which just rethrows the error: + // https://github.com/nestjs/nest/blob/master/packages/core/exceptions/external-exception-filter.ts + if (host.getType<'graphql'>() === 'graphql') { + // neither report nor log HttpExceptions + if (exception instanceof HttpException) { + throw exception; + } + + if (exception instanceof Error) { + this._logger.error(exception.message, exception.stack); + } + + captureException(exception); + throw exception; + } + + if (!isExpectedError(exception)) { + captureException(exception); } - captureException(exception); return super.catch(exception, host); } } @@ -89,13 +107,7 @@ Catch()(SentryGlobalFilter); export { SentryGlobalFilter }; /** - * Global filter to handle exceptions and report them to Sentry. - * - * The BaseExceptionFilter does not work well in GraphQL applications. - * By default, Nest GraphQL applications use the ExternalExceptionFilter, which just rethrows the error: - * https://github.com/nestjs/nest/blob/master/packages/core/exceptions/external-exception-filter.ts - * - * The ExternalExceptinFilter is not exported, so we reimplement this filter here. + * Global filter to handle exceptions in NestJS + GraphQL applications and report them to Sentry. */ class SentryGlobalGraphQLFilter { private static readonly _logger = new Logger('ExceptionsHandler'); @@ -129,29 +141,7 @@ export { SentryGlobalGraphQLFilter }; * * This filter is a generic filter that can handle both HTTP and GraphQL exceptions. */ -class SentryGlobalGenericFilter extends SentryGlobalFilter { - public readonly __SENTRY_INTERNAL__: boolean; - private readonly _graphqlFilter: SentryGlobalGraphQLFilter; - - public constructor(applicationRef?: HttpServer) { - super(applicationRef); - this.__SENTRY_INTERNAL__ = true; - this._graphqlFilter = new SentryGlobalGraphQLFilter(); - } - - /** - * Catches exceptions and forwards them to the according error filter. - */ - public catch(exception: unknown, host: ArgumentsHost): void { - if (host.getType<'graphql'>() === 'graphql') { - return this._graphqlFilter.catch(exception, host); - } - - super.catch(exception, host); - } -} -Catch()(SentryGlobalGenericFilter); -export { SentryGlobalGenericFilter }; +export const SentryGlobalGenericFilter = SentryGlobalFilter; /** * Service to set up Sentry performance tracing for Nest.js applications.