Skip to content

Commit 42708e0

Browse files
onurtemizkanAbhiPrasad
authored andcommitted
fix(node): Skip capturing Hapi Boom error responses. (#11151)
Resolves: #11069 After checking the behaviour, it seems to me that we don't need to capture any Boom responses, as the errors that may cause a `5xx` response are already captured by the core logic. To add an option to control this behaviour, we need to update the usage of `hapiErrorPlugin`, converting it to a function signature, which IMO may not worth doing, as I'm not sure if users in general would need to use it. This also adds `expectError()` to the `node-integration-tests` runner to allow `5xx` responses to be tested.
1 parent 16dc092 commit 42708e0

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

dev-packages/node-integration-tests/suites/tracing-experimental/hapi/scenario.js

+26
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Sentry.init({
1010
});
1111

1212
const Hapi = require('@hapi/hapi');
13+
const Boom = require('@hapi/boom');
1314

1415
const port = 5999;
1516

@@ -27,6 +28,31 @@ const init = async () => {
2728
},
2829
});
2930

31+
server.route({
32+
method: 'GET',
33+
path: '/error',
34+
handler: (_request, _h) => {
35+
return new Error('Sentry Test Error');
36+
},
37+
});
38+
39+
server.route({
40+
method: 'GET',
41+
path: '/boom-error',
42+
handler: (_request, _h) => {
43+
return new Boom.Boom('Sentry Test Error');
44+
},
45+
});
46+
47+
server.route({
48+
method: 'GET',
49+
path: '/promise-error',
50+
handler: async (_request, _h) => {
51+
return Promise.reject(new Error('Sentry Test Error'));
52+
},
53+
});
54+
55+
await Sentry.setupHapiErrorHandler(server);
3056
await server.start();
3157

3258
sendPortToRunner(port);

dev-packages/node-integration-tests/suites/tracing-experimental/hapi/test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,45 @@ conditionalTest({ min: 14 })('hapi auto-instrumentation', () => {
2626
]),
2727
};
2828

29+
const EXPECTED_ERROR_EVENT = {
30+
exception: {
31+
values: [
32+
{
33+
type: 'Error',
34+
value: 'Sentry Test Error',
35+
},
36+
],
37+
},
38+
};
39+
2940
test('CJS - should auto-instrument `@hapi/hapi` package.', done => {
3041
createRunner(__dirname, 'scenario.js')
3142
.expect({ transaction: EXPECTED_TRANSACTION })
3243
.start(done)
3344
.makeRequest('get', '/');
3445
});
46+
47+
test('CJS - should handle returned plain errors in routes.', done => {
48+
createRunner(__dirname, 'scenario.js')
49+
.expect({ event: EXPECTED_ERROR_EVENT })
50+
.expectError()
51+
.start(done)
52+
.makeRequest('get', '/error');
53+
});
54+
55+
test('CJS - should handle returned Boom errors in routes.', done => {
56+
createRunner(__dirname, 'scenario.js')
57+
.expect({ event: EXPECTED_ERROR_EVENT })
58+
.expectError()
59+
.start(done)
60+
.makeRequest('get', '/boom-error');
61+
});
62+
63+
test('CJS - should handle promise rejections in routes.', done => {
64+
createRunner(__dirname, 'scenario.js')
65+
.expect({ event: EXPECTED_ERROR_EVENT })
66+
.expectError()
67+
.start(done)
68+
.makeRequest('get', '/promise-error');
69+
});
3570
});

dev-packages/node-integration-tests/utils/runner.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export function createRunner(...paths: string[]) {
126126
let withSentryServer = false;
127127
let dockerOptions: DockerOptions | undefined;
128128
let ensureNoErrorOutput = false;
129+
let expectError = false;
129130

130131
if (testPath.endsWith('.ts')) {
131132
flags.push('-r', 'ts-node/register');
@@ -136,6 +137,10 @@ export function createRunner(...paths: string[]) {
136137
expectedEnvelopes.push(expected);
137138
return this;
138139
},
140+
expectError: function () {
141+
expectError = true;
142+
return this;
143+
},
139144
withFlags: function (...args: string[]) {
140145
flags.push(...args);
141146
return this;
@@ -347,7 +352,18 @@ export function createRunner(...paths: string[]) {
347352
}
348353

349354
const url = `http://localhost:${scenarioServerPort}${path}`;
350-
if (method === 'get') {
355+
if (expectError) {
356+
try {
357+
if (method === 'get') {
358+
await axios.get(url, { headers });
359+
} else {
360+
await axios.post(url, { headers });
361+
}
362+
} catch (e) {
363+
return;
364+
}
365+
return;
366+
} else if (method === 'get') {
351367
return (await axios.get(url, { headers })).data;
352368
} else {
353369
return (await axios.post(url, { headers })).data;

packages/node/src/integrations/hapi/index.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ function isResponseObject(response: ResponseObject | Boom): response is Response
2020
return response && (response as ResponseObject).statusCode !== undefined;
2121
}
2222

23-
function isBoomObject(response: ResponseObject | Boom): response is Boom {
24-
return response && (response as Boom).isBoom !== undefined;
25-
}
26-
2723
function isErrorEvent(event: RequestEvent): event is RequestEvent {
2824
return event && (event as RequestEvent).error !== undefined;
2925
}
@@ -51,9 +47,7 @@ export const hapiErrorPlugin = {
5147
// eslint-disable-next-line deprecation/deprecation
5248
const transaction = getActiveTransaction();
5349

54-
if (request.response && isBoomObject(request.response)) {
55-
sendErrorToSentry(request.response);
56-
} else if (isErrorEvent(event)) {
50+
if (isErrorEvent(event)) {
5751
sendErrorToSentry(event.error);
5852
}
5953

0 commit comments

Comments
 (0)