Skip to content

Commit

Permalink
fix(node): Skip capturing Hapi Boom error responses. (#11151)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
onurtemizkan committed Mar 25, 2024
1 parent e057674 commit 87eed51
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Sentry.init({
});

const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');

const port = 5999;

Expand All @@ -26,6 +27,30 @@ const init = async () => {
},
});

server.route({
method: 'GET',
path: '/error',
handler: (_request, _h) => {
return new Error('Sentry Test Error');
},
});

server.route({
method: 'GET',
path: '/boom-error',
handler: (_request, _h) => {
return new Boom.Boom('Sentry Test Error');
},
});

server.route({
method: 'GET',
path: '/promise-error',
handler: async (_request, _h) => {
return Promise.reject(new Error('Sentry Test Error'));
},
});

await Sentry.setupHapiErrorHandler(server);
await server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,45 @@ describe('hapi auto-instrumentation', () => {
]),
};

const EXPECTED_ERROR_EVENT = {
exception: {
values: [
{
type: 'Error',
value: 'Sentry Test Error',
},
],
},
};

test('CJS - should auto-instrument `@hapi/hapi` package.', done => {
createRunner(__dirname, 'scenario.js')
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done)
.makeRequest('get', '/');
});

test('CJS - should handle returned plain errors in routes.', done => {
createRunner(__dirname, 'scenario.js')
.expect({ event: EXPECTED_ERROR_EVENT })
.expectError()
.start(done)
.makeRequest('get', '/error');
});

test('CJS - should handle returned Boom errors in routes.', done => {
createRunner(__dirname, 'scenario.js')
.expect({ event: EXPECTED_ERROR_EVENT })
.expectError()
.start(done)
.makeRequest('get', '/boom-error');
});

test('CJS - should handle promise rejections in routes.', done => {
createRunner(__dirname, 'scenario.js')
.expect({ event: EXPECTED_ERROR_EVENT })
.expectError()
.start(done)
.makeRequest('get', '/promise-error');
});
});
18 changes: 17 additions & 1 deletion dev-packages/node-integration-tests/utils/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function createRunner(...paths: string[]) {
let withSentryServer = false;
let dockerOptions: DockerOptions | undefined;
let ensureNoErrorOutput = false;
let expectError = false;

if (testPath.endsWith('.ts')) {
flags.push('-r', 'ts-node/register');
Expand All @@ -136,6 +137,10 @@ export function createRunner(...paths: string[]) {
expectedEnvelopes.push(expected);
return this;
},
expectError: function () {
expectError = true;
return this;
},
withFlags: function (...args: string[]) {
flags.push(...args);
return this;
Expand Down Expand Up @@ -347,7 +352,18 @@ export function createRunner(...paths: string[]) {
}

const url = `http://localhost:${scenarioServerPort}${path}`;
if (method === 'get') {
if (expectError) {
try {
if (method === 'get') {
await axios.get(url, { headers });
} else {
await axios.post(url, { headers });
}
} catch (e) {
return;
}
return;
} else if (method === 'get') {
return (await axios.get(url, { headers })).data;
} else {
return (await axios.post(url, { headers })).data;
Expand Down
8 changes: 1 addition & 7 deletions packages/node/src/integrations/hapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ function isResponseObject(response: ResponseObject | Boom): response is Response
return response && (response as ResponseObject).statusCode !== undefined;
}

function isBoomObject(response: ResponseObject | Boom): response is Boom {
return response && (response as Boom).isBoom !== undefined;
}

function isErrorEvent(event: RequestEvent): event is RequestEvent {
return event && (event as RequestEvent).error !== undefined;
}
Expand Down Expand Up @@ -54,9 +50,7 @@ export const hapiErrorPlugin = {
const activeSpan = getActiveSpan();
const rootSpan = activeSpan && getRootSpan(activeSpan);

if (request.response && isBoomObject(request.response)) {
sendErrorToSentry(request.response);
} else if (isErrorEvent(event)) {
if (isErrorEvent(event)) {
sendErrorToSentry(event.error);
}

Expand Down

0 comments on commit 87eed51

Please sign in to comment.