Skip to content

fix(node): Fix nest.js error handler #11874

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
May 2, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NestFactory } from '@nestjs/core';
import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';
import * as Sentry from '@sentry/node';
import { AppModule1, AppModule2 } from './app.module';

Expand All @@ -15,7 +15,9 @@ async function bootstrap() {
});

const app1 = await NestFactory.create(AppModule1);
Sentry.setupNestErrorHandler(app1);

const { httpAdapter } = app1.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(app1, new BaseExceptionFilter(httpAdapter));

await app1.listen(app1Port);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ test('Sends exception to Sentry', async ({ baseURL }) => {
});

try {
axios.get(`${baseURL}/test-exception/123`);
} catch {
// this results in an error, but we don't care - we want to check the error event
await axios.get(`${baseURL}/test-exception/123`);
// Should never be reached!
expect(false).toBe(true);
} catch (error) {
expect(error).toBeInstanceOf(AxiosError);
expect(error.response?.status).toBe(500);
}

const errorEvent = await errorEventPromise;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Sentry.init({
});

import { Controller, Get, Injectable, Module, Param } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';

const port = 3480;

Expand Down Expand Up @@ -49,7 +49,8 @@ class AppModule {}

async function init(): Promise<void> {
const app = await NestFactory.create(AppModule);
Sentry.setupNestErrorHandler(app);
const { httpAdapter } = app.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
await app.listen(port);
sendPortToRunner(port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Sentry.init({
});

import { Controller, Get, Injectable, Module, Param } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';

const port = 3460;

Expand Down Expand Up @@ -47,7 +47,8 @@ class AppModule {}

async function init(): Promise<void> {
const app = await NestFactory.create(AppModule);
Sentry.setupNestErrorHandler(app);
const { httpAdapter } = app.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
await app.listen(port);
sendPortToRunner(port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Sentry.init({
});

import { Controller, Get, Injectable, Module, Param } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';

const port = 3470;

Expand Down Expand Up @@ -49,7 +49,8 @@ class AppModule {}

async function init(): Promise<void> {
const app = await NestFactory.create(AppModule);
Sentry.setupNestErrorHandler(app);
const { httpAdapter } = app.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
await app.listen(port);
sendPortToRunner(port);
}
Expand Down
32 changes: 23 additions & 9 deletions packages/node/src/integrations/tracing/nest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ interface MinimalNestJsExecutionContext {
};
};
}

interface NestJsErrorFilter {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
catch(exception: any, host: any): void;
}

interface MinimalNestJsApp {
useGlobalFilters: (arg0: { catch(exception: unknown): void }) => void;
useGlobalFilters: (arg0: NestJsErrorFilter) => void;
useGlobalInterceptors: (interceptor: {
intercept: (context: MinimalNestJsExecutionContext, next: { handle: () => void }) => void;
}) => void;
Expand All @@ -40,16 +46,10 @@ const _nestIntegration = (() => {
*/
export const nestIntegration = defineIntegration(_nestIntegration);

const SentryNestExceptionFilter = {
catch(exception: unknown) {
captureException(exception);
},
};

/**
* Setup an error handler for Nest.
*/
export function setupNestErrorHandler(app: MinimalNestJsApp): void {
export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsErrorFilter): void {
app.useGlobalInterceptors({
intercept(context, next) {
if (getIsolationScope() === getDefaultIsolationScope()) {
Expand All @@ -65,5 +65,19 @@ export function setupNestErrorHandler(app: MinimalNestJsApp): void {
},
});

app.useGlobalFilters(SentryNestExceptionFilter);
const wrappedFilter = new Proxy(baseFilter, {
get(target, prop, receiver) {
if (prop === 'catch') {
const originalCatch = Reflect.get(target, prop, receiver);

return (exception: unknown, host: unknown) => {
captureException(exception);
return originalCatch.apply(target, [exception, host]);
};
}
return Reflect.get(target, prop, receiver);
},
});

app.useGlobalFilters(wrappedFilter);
}
Loading