From 41c51e9cf9f8a3c152454d54a0e3d86fdcff8a21 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Mon, 24 Jun 2024 15:09:35 +0200 Subject: [PATCH] fix(node): Re-throw errors from koa middleware (#12609) Fixes #12517 --- .../test-applications/node-koa/index.js | 6 ++++ .../node-koa/tests/assert.test.ts | 35 +++++++++++++++++++ packages/node/src/integrations/tracing/koa.ts | 1 + 3 files changed, 42 insertions(+) create mode 100644 dev-packages/e2e-tests/test-applications/node-koa/tests/assert.test.ts diff --git a/dev-packages/e2e-tests/test-applications/node-koa/index.js b/dev-packages/e2e-tests/test-applications/node-koa/index.js index 08ddc231ffb3..ddc17f62e6f7 100644 --- a/dev-packages/e2e-tests/test-applications/node-koa/index.js +++ b/dev-packages/e2e-tests/test-applications/node-koa/index.js @@ -103,6 +103,12 @@ router1.get('/test-outgoing-http-external-disallowed', async ctx => { ctx.body = data; }); +router1.get('/test-assert/:condition', async ctx => { + ctx.body = 200; + const condition = ctx.params.condition !== 'false'; + ctx.assert(condition, 400, 'ctx.assert failed'); +}); + app1.use(router1.routes()).use(router1.allowedMethods()); app1.listen(port1); diff --git a/dev-packages/e2e-tests/test-applications/node-koa/tests/assert.test.ts b/dev-packages/e2e-tests/test-applications/node-koa/tests/assert.test.ts new file mode 100644 index 000000000000..0f9f724ef237 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/node-koa/tests/assert.test.ts @@ -0,0 +1,35 @@ +import { expect, test } from '@playwright/test'; +import { waitForError } from '@sentry-internal/test-utils'; + +test('Returns 400 from failed assert', async ({ baseURL }) => { + const errorEventPromise = waitForError('node-koa', event => { + return !event.type && event.exception?.values?.[0]?.value === 'ctx.assert failed'; + }); + + const res = await fetch(`${baseURL}/test-assert/false`); + expect(res.status).toBe(400); + + const errorEvent = await errorEventPromise; + + expect(errorEvent.exception?.values).toHaveLength(1); + expect(errorEvent.exception?.values?.[0]?.value).toBe('ctx.assert failed'); + + expect(errorEvent.request).toEqual({ + method: 'GET', + cookies: {}, + headers: expect.any(Object), + url: 'http://localhost:3030/test-assert/false', + }); + + expect(errorEvent.transaction).toEqual('GET /test-assert/:condition'); + + expect(errorEvent.contexts?.trace).toEqual({ + trace_id: expect.any(String), + span_id: expect.any(String), + }); +}); + +test('Returns 200 from successful assert', async ({ baseURL }) => { + const res = await fetch(`${baseURL}/test-assert/true`); + expect(res.status).toBe(200); +}); diff --git a/packages/node/src/integrations/tracing/koa.ts b/packages/node/src/integrations/tracing/koa.ts index 1fc85234fb76..071238eb7094 100644 --- a/packages/node/src/integrations/tracing/koa.ts +++ b/packages/node/src/integrations/tracing/koa.ts @@ -56,6 +56,7 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) => await next(); } catch (error) { captureException(error); + throw error; } });