Skip to content

Commit afd527c

Browse files
committed
Allow usage on runtimes with no Error.captureStackTrace()
1 parent 5e0d68f commit afd527c

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ exports.Boom = class Boom extends Error {
8383
const { statusCode = 500, data, headers, decorate, ctor = exports.Boom, cause } = options;
8484

8585
super(message ?? internals.codes.get(statusCode) ?? 'Unknown', { cause });
86-
Error.captureStackTrace(this, ctor); // Filter the stack to our external API
86+
if (typeof Error.captureStackTrace === 'function') { // Only use when available
87+
Error.captureStackTrace(this, ctor); // Filter the stack to our external API
88+
}
8789

8890
if (cause !== undefined) {
8991
this.cause ??= cause; // Explicitly assign cause to work with old runtimes

test/index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,22 +1013,40 @@ describe('Boom', () => {
10131013

10141014
describe('stack trace', () => {
10151015

1016+
const helpers = ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed',
1017+
'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict',
1018+
'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge',
1019+
'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed',
1020+
'badData', 'preconditionRequired', 'tooManyRequests',
1021+
1022+
// 500s
1023+
'internal', 'notImplemented', 'badGateway', 'serverUnavailable',
1024+
'gatewayTimeout', 'badImplementation'
1025+
];
1026+
10161027
it('should omit lib', () => {
10171028

1018-
['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed',
1019-
'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict',
1020-
'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge',
1021-
'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed',
1022-
'badData', 'preconditionRequired', 'tooManyRequests',
1029+
for (const helper of helpers) {
1030+
const err = Boom[helper]();
1031+
expect(err.stack).to.not.match(/\/lib\/index\.js/);
1032+
}
1033+
});
10231034

1024-
// 500s
1025-
'internal', 'notImplemented', 'badGateway', 'serverUnavailable',
1026-
'gatewayTimeout', 'badImplementation'
1027-
].forEach((name) => {
1035+
it('should not crash when Error.captureStackTrace is missing', (flags) => {
10281036

1029-
const err = Boom[name]();
1030-
expect(err.stack).to.not.match(/\/lib\/index\.js/);
1031-
});
1037+
const captureStackTrace = Error.captureStackTrace;
1038+
1039+
for (const helper of helpers) {
1040+
try {
1041+
Error.captureStackTrace = undefined;
1042+
var err = Boom[helper]();
1043+
}
1044+
finally {
1045+
Error.captureStackTrace = captureStackTrace;
1046+
}
1047+
1048+
expect(err.stack).to.match(/\/lib\/index\.js/);
1049+
}
10321050
});
10331051
});
10341052

0 commit comments

Comments
 (0)