Skip to content

Commit

Permalink
fix(ses): Trap and report errors
Browse files Browse the repository at this point in the history
Ensures that any error that bottoms out the stack of an event in Node.js or on the web gets reported through the tamed console so its stack can be revealed.

Fixes #769
  • Loading branch information
kriskowal committed Jun 30, 2021
1 parent b6fe46f commit a79df15
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
/* global process, window */

import { loggedErrorHandler as defaultHandler } from './assert.js';
import { makeCausalConsole } from './console.js';
Expand Down Expand Up @@ -37,5 +38,34 @@ export const tameConsole = (
};
}
const causalConsole = makeCausalConsole(originalConsole, loggedErrorHandler);

// Attach platform-specific error traps such that any error that bottoms out
// an event will get unwrapped by the causal console, revealing the error
// stack to the console for debugging.

// Node.js
if (
typeof process === 'object' &&
process !== null &&
typeof process.on === 'function' &&
typeof process.exit === 'function'
) {
process.on('uncaughtException', error => {
causalConsole.error(error);
process.exit(process.exitCode || -1);
});
}

// Web
if (
typeof window === 'object' &&
window !== null &&
typeof window.addEventListener === 'function'
) {
window.addEventListener('error', event => {
causalConsole.error(event.error);
});
}

return { console: causalConsole };
};

0 comments on commit a79df15

Please sign in to comment.