Skip to content

Commit

Permalink
feat(ses): Fail fast when a required process.exit or process.abort me…
Browse files Browse the repository at this point in the history
…thod is missing
  • Loading branch information
gibson042 committed Jan 8, 2024
1 parent bc1e2dc commit 5d637d0
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

import {
// Using TypeError minimizes risk of exposing the feral Error constructor
TypeError,
apply,
defineProperty,
Expand Down Expand Up @@ -101,13 +102,27 @@ export const tameConsole = (
typeof globalProcess === 'object' &&
typeof globalProcess.on === 'function'
) {
let terminate;
if (errorTrapping === 'platform' || errorTrapping === 'exit') {
const { exit } = globalProcess;
if (typeof exit !== 'function') {
// There is a function-valued process.on but no function-valued process.exit;
// fail early without caring whether errorTrapping is "platform" only by default.
throw TypeError('missing process.exit');
}
terminate = () => exit(globalProcess.exitCode || -1);
} else if (errorTrapping === 'abort') {
terminate = globalProcess.abort;
if (typeof terminate !== 'function') {
throw TypeError('missing process.abort');
}
}

globalProcess.on('uncaughtException', error => {
// causalConsole is born frozen so not vulnerable to method tampering.
ourConsole.error(error);
if (errorTrapping === 'platform' || errorTrapping === 'exit') {
globalProcess.exit(globalProcess.exitCode || -1);
} else if (errorTrapping === 'abort') {
globalProcess.abort();
if (terminate) {
terminate();
}
});
}
Expand Down

0 comments on commit 5d637d0

Please sign in to comment.