Skip to content

Commit 41c51e9

Browse files
authored
fix(node): Re-throw errors from koa middleware (#12609)
Fixes #12517
1 parent 85debda commit 41c51e9

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

dev-packages/e2e-tests/test-applications/node-koa/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ router1.get('/test-outgoing-http-external-disallowed', async ctx => {
103103
ctx.body = data;
104104
});
105105

106+
router1.get('/test-assert/:condition', async ctx => {
107+
ctx.body = 200;
108+
const condition = ctx.params.condition !== 'false';
109+
ctx.assert(condition, 400, 'ctx.assert failed');
110+
});
111+
106112
app1.use(router1.routes()).use(router1.allowedMethods());
107113

108114
app1.listen(port1);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForError } from '@sentry-internal/test-utils';
3+
4+
test('Returns 400 from failed assert', async ({ baseURL }) => {
5+
const errorEventPromise = waitForError('node-koa', event => {
6+
return !event.type && event.exception?.values?.[0]?.value === 'ctx.assert failed';
7+
});
8+
9+
const res = await fetch(`${baseURL}/test-assert/false`);
10+
expect(res.status).toBe(400);
11+
12+
const errorEvent = await errorEventPromise;
13+
14+
expect(errorEvent.exception?.values).toHaveLength(1);
15+
expect(errorEvent.exception?.values?.[0]?.value).toBe('ctx.assert failed');
16+
17+
expect(errorEvent.request).toEqual({
18+
method: 'GET',
19+
cookies: {},
20+
headers: expect.any(Object),
21+
url: 'http://localhost:3030/test-assert/false',
22+
});
23+
24+
expect(errorEvent.transaction).toEqual('GET /test-assert/:condition');
25+
26+
expect(errorEvent.contexts?.trace).toEqual({
27+
trace_id: expect.any(String),
28+
span_id: expect.any(String),
29+
});
30+
});
31+
32+
test('Returns 200 from successful assert', async ({ baseURL }) => {
33+
const res = await fetch(`${baseURL}/test-assert/true`);
34+
expect(res.status).toBe(200);
35+
});

packages/node/src/integrations/tracing/koa.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) =>
5656
await next();
5757
} catch (error) {
5858
captureException(error);
59+
throw error;
5960
}
6061
});
6162

0 commit comments

Comments
 (0)