Skip to content

Commit 87eed51

Browse files
authored
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 e057674 commit 87eed51

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Sentry.init({
99
});
1010

1111
const Hapi = require('@hapi/hapi');
12+
const Boom = require('@hapi/boom');
1213

1314
const port = 5999;
1415

@@ -26,6 +27,30 @@ const init = async () => {
2627
},
2728
});
2829

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

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

+35
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,45 @@ describe('hapi auto-instrumentation', () => {
2525
]),
2626
};
2727

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

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
@@ -23,10 +23,6 @@ function isResponseObject(response: ResponseObject | Boom): response is Response
2323
return response && (response as ResponseObject).statusCode !== undefined;
2424
}
2525

26-
function isBoomObject(response: ResponseObject | Boom): response is Boom {
27-
return response && (response as Boom).isBoom !== undefined;
28-
}
29-
3026
function isErrorEvent(event: RequestEvent): event is RequestEvent {
3127
return event && (event as RequestEvent).error !== undefined;
3228
}
@@ -54,9 +50,7 @@ export const hapiErrorPlugin = {
5450
const activeSpan = getActiveSpan();
5551
const rootSpan = activeSpan && getRootSpan(activeSpan);
5652

57-
if (request.response && isBoomObject(request.response)) {
58-
sendErrorToSentry(request.response);
59-
} else if (isErrorEvent(event)) {
53+
if (isErrorEvent(event)) {
6054
sendErrorToSentry(event.error);
6155
}
6256

0 commit comments

Comments
 (0)