diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/app.module.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/app.module.ts index b4f9d5588dda..932d1af99611 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs/src/app.module.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/app.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { ScheduleModule } from '@nestjs/schedule'; import { AppController1, AppController2 } from './app.controller'; import { AppService1, AppService2 } from './app.service'; +import { TestModule } from './test-module/test.module'; @Module({ - imports: [ScheduleModule.forRoot()], + imports: [ScheduleModule.forRoot(), TestModule], controllers: [AppController1], providers: [AppService1], }) diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.controller.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.controller.ts new file mode 100644 index 000000000000..150fb0e07546 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.controller.ts @@ -0,0 +1,12 @@ +import { Controller, Get } from '@nestjs/common'; +import { TestException } from './test.exception'; + +@Controller('test-module') +export class TestController { + constructor() {} + + @Get() + getTest(): string { + throw new TestException(); + } +} diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.exception.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.exception.ts new file mode 100644 index 000000000000..b736596b6717 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.exception.ts @@ -0,0 +1,5 @@ +export class TestException extends Error { + constructor() { + super('Something went wrong in the test module!'); + } +} diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.filter.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.filter.ts new file mode 100644 index 000000000000..87a4ca0920e5 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.filter.ts @@ -0,0 +1,13 @@ +import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common'; +import { BaseExceptionFilter } from '@nestjs/core'; +import { TestException } from './test.exception'; + +@Catch(TestException) +export class TestExceptionFilter extends BaseExceptionFilter { + catch(exception: unknown, host: ArgumentsHost) { + if (exception instanceof TestException) { + return super.catch(new BadRequestException(exception.message), host); + } + return super.catch(exception, host); + } +} diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.module.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.module.ts new file mode 100644 index 000000000000..37b6dbe7e819 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { APP_FILTER } from '@nestjs/core'; +import { TestController } from './test.controller'; +import { TestExceptionFilter } from './test.filter'; + +@Module({ + imports: [], + controllers: [TestController], + providers: [ + { + provide: APP_FILTER, + useClass: TestExceptionFilter, + }, + ], +}) +export class TestModule {} diff --git a/dev-packages/e2e-tests/test-applications/nestjs/tests/errors.test.ts b/dev-packages/e2e-tests/test-applications/nestjs/tests/errors.test.ts index 349b25b0eee9..ffb48f4e5e70 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs/tests/errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs/tests/errors.test.ts @@ -53,3 +53,32 @@ test('Does not send expected exception to Sentry', async ({ baseURL }) => { expect(errorEventOccurred).toBe(false); }); + +test('Does not handle expected exception if exception is thrown in module', async ({ baseURL }) => { + const errorEventPromise = waitForError('nestjs', event => { + return !event.type && event.exception?.values?.[0]?.value === 'Something went wrong in the test module!'; + }); + + const response = await fetch(`${baseURL}/test-module`); + expect(response.status).toBe(500); // should be 400 + + // should never arrive, but does because the exception is not handled properly + const errorEvent = await errorEventPromise; + + expect(errorEvent.exception?.values).toHaveLength(1); + expect(errorEvent.exception?.values?.[0]?.value).toBe('Something went wrong in the test module!'); + + expect(errorEvent.request).toEqual({ + method: 'GET', + cookies: {}, + headers: expect.any(Object), + url: 'http://localhost:3030/test-module', + }); + + expect(errorEvent.transaction).toEqual('GET /test-module'); + + expect(errorEvent.contexts?.trace).toEqual({ + trace_id: expect.any(String), + span_id: expect.any(String), + }); +});