From 01d96a36dd6f1da9a7c9bfb3853d8a5e6c51a590 Mon Sep 17 00:00:00 2001 From: nicohrubec Date: Thu, 11 Jul 2024 09:56:48 +0200 Subject: [PATCH 1/2] Add e2e documenting unexpected behavior in nest --- .../nestjs/src/app.module.ts | 3 +- .../nestjs/src/test-module/test.controller.ts | 12 ++++++++ .../nestjs/src/test-module/test.exception.ts | 5 ++++ .../nestjs/src/test-module/test.filter.ts | 13 +++++++++ .../nestjs/src/test-module/test.module.ts | 16 ++++++++++ .../nestjs/tests/errors.test.ts | 29 +++++++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.controller.ts create mode 100644 dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.exception.ts create mode 100644 dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.filter.ts create mode 100644 dev-packages/e2e-tests/test-applications/nestjs/src/test-module/test.module.ts 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..e94203ead201 --- /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..aba60d2b1fa1 --- /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 { TestException } from './test.exception'; +import { BaseExceptionFilter } from '@nestjs/core'; + +@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..3b986c03ab22 --- /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 { TestController } from './test.controller'; +import { APP_FILTER } from '@nestjs/core'; +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..23eb165cd580 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), + }); +}); From d1a35315d920ee2ff2e3b3abeb2e50a4b1639488 Mon Sep 17 00:00:00 2001 From: nicohrubec Date: Thu, 11 Jul 2024 10:06:33 +0200 Subject: [PATCH 2/2] Lint --- .../nestjs/src/test-module/test.exception.ts | 2 +- .../test-applications/nestjs/src/test-module/test.filter.ts | 4 ++-- .../test-applications/nestjs/src/test-module/test.module.ts | 6 +++--- .../e2e-tests/test-applications/nestjs/tests/errors.test.ts | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) 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 index e94203ead201..b736596b6717 100644 --- 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 @@ -1,5 +1,5 @@ export class TestException extends Error { constructor() { - super("Something went wrong in the test module!"); + 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 index aba60d2b1fa1..87a4ca0920e5 100644 --- 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 @@ -1,10 +1,10 @@ import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common'; -import { TestException } from './test.exception'; import { BaseExceptionFilter } from '@nestjs/core'; +import { TestException } from './test.exception'; @Catch(TestException) export class TestExceptionFilter extends BaseExceptionFilter { - catch (exception: unknown, host: ArgumentsHost) { + catch(exception: unknown, host: ArgumentsHost) { if (exception instanceof TestException) { return super.catch(new BadRequestException(exception.message), 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 index 3b986c03ab22..37b6dbe7e819 100644 --- 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 @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; -import { TestController } from './test.controller'; import { APP_FILTER } from '@nestjs/core'; +import { TestController } from './test.controller'; import { TestExceptionFilter } from './test.filter'; @Module({ @@ -10,7 +10,7 @@ import { TestExceptionFilter } from './test.filter'; { 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 23eb165cd580..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 @@ -57,7 +57,7 @@ test('Does not send expected exception to Sentry', async ({ baseURL }) => { 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