Skip to content

Commit

Permalink
fix(node): Re-throw errors from koa middleware (#12609)
Browse files Browse the repository at this point in the history
Fixes #12517
  • Loading branch information
chargome committed Jun 24, 2024
1 parent 85debda commit 41c51e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dev-packages/e2e-tests/test-applications/node-koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) =>
await next();
} catch (error) {
captureException(error);
throw error;
}
});

Expand Down

0 comments on commit 41c51e9

Please sign in to comment.