Skip to content

Commit 39698b0

Browse files
committed
fixup! test(fastify): Verify if upstream error is fixed and won't regress
1 parent 059c305 commit 39698b0

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

dev-packages/e2e-tests/test-applications/node-fastify-5/src/app.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ Sentry.init({
2525
return false;
2626
}
2727

28+
// @ts-ignore // Fastify V5 is not typed correctly
29+
if (_request.routeOptions?.url?.includes('/test-error-ignored') && _reply.statusCode === 500) {
30+
return false;
31+
}
32+
2833
return true;
2934
},
3035
}),
@@ -90,6 +95,21 @@ app.get('/test-error', async function (req, res) {
9095
res.send({ exceptionId });
9196
});
9297

98+
// Regression test for https://github.com/fastify/fastify/issues/6409
99+
// The error diagnostic channel was always sending 200 unless explicitly changed.
100+
// This was fixed in Fastify 5.7.0
101+
app.register((childApp: F.FastifyInstance, _options: F.FastifyPluginOptions, next: (err?: Error) => void) => {
102+
childApp.setErrorHandler((error: Error, _request: F.FastifyRequest, reply: F.FastifyReply) => {
103+
reply.send({ ok: false });
104+
});
105+
106+
childApp.get('/test-error-ignored', async function () {
107+
throw new Error('This is an error that will not be captured');
108+
});
109+
110+
next();
111+
});
112+
93113
app.get('/test-error-not-captured', async function () {
94114
// This error will not be captured by Sentry
95115
throw new Error('This is an error that will not be captured');
@@ -127,6 +147,11 @@ app.post('/test-post', function (req, res) {
127147
res.send({ status: 'ok', body: req.body });
128148
});
129149

150+
app.get('/flush', async function (_req, res) {
151+
await Sentry.flush();
152+
res.send({ ok: true });
153+
});
154+
130155
app.listen({ port: port });
131156

132157
// A second app so we can test header propagation between external URLs

dev-packages/e2e-tests/test-applications/node-fastify-5/tests/errors.test.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,51 @@ test('Sends correct error event', async ({ baseURL }) => {
3535
});
3636

3737
test('Does not send error when shouldHandleError returns false', async ({ baseURL }) => {
38-
const errorEventPromise = waitForError('node-fastify-5', event => {
39-
return !event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured';
38+
let errorEventOccurred = false;
39+
40+
waitForError('node-fastify-5', event => {
41+
if (!event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured') {
42+
errorEventOccurred = true;
43+
}
44+
return event?.transaction === 'GET /test-error-not-captured';
4045
});
4146

42-
errorEventPromise.then(() => {
43-
throw new Error('This error should not be captured');
47+
const transactionEventPromise = waitForTransaction('node-fastify-5', transactionEvent => {
48+
return transactionEvent?.transaction === 'GET /test-error-not-captured';
4449
});
4550

4651
await fetch(`${baseURL}/test-error-not-captured`);
4752

48-
// wait for a short time to ensure the error is not captured
49-
await new Promise(resolve => setTimeout(resolve, 1000));
53+
await transactionEventPromise;
54+
55+
await fetch(`${baseURL}/flush`);
56+
57+
expect(errorEventOccurred).toBe(false);
5058
});
5159

5260
// Regression test for https://github.com/fastify/fastify/issues/6409
5361
// The error diagnostic channel was always sending 200 unless explicitly changed.
5462
// This was fixed in Fastify 5.7.0
5563
test('Error in child plugin with rethrown error handler reports correct 500 status', async ({ baseURL }) => {
56-
const errorEventPromise = waitForError('node-fastify-5', event => {
57-
return !event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured';
64+
let errorEventOccurred = false;
65+
66+
waitForError('node-fastify-5', event => {
67+
if (!event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured') {
68+
errorEventOccurred = true;
69+
}
70+
return event?.transaction === 'GET /test-error-ignored';
5871
});
5972

60-
errorEventPromise.then(() => {
61-
throw new Error('This error should not be captured');
73+
const transactionEventPromise = waitForTransaction('node-fastify-5', transactionEvent => {
74+
return transactionEvent?.transaction === 'GET /test-error-ignored';
6275
});
6376

77+
6478
await fetch(`${baseURL}/test-error-ignored`);
6579

66-
// wait for a short time to ensure the error is not captured
67-
await new Promise(resolve => setTimeout(resolve, 1000));
80+
await transactionEventPromise;
81+
82+
await fetch(`${baseURL}/flush`);
83+
84+
expect(errorEventOccurred).toBe(false);
6885
});

0 commit comments

Comments
 (0)