From fece445f0191e41c1324d26d83d830dfa8822400 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Wed, 11 Oct 2023 21:28:48 -0400 Subject: [PATCH] fix(ses): Support absence of console Fix #1819 --- packages/ses/src/error/console.js | 4 ++++ packages/ses/src/error/internal-types.js | 4 ++-- packages/ses/src/error/tame-console.js | 11 ++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/ses/src/error/console.js b/packages/ses/src/error/console.js index e11b697e96..0efd6fb15b 100644 --- a/packages/ses/src/error/console.js +++ b/packages/ses/src/error/console.js @@ -174,6 +174,10 @@ freeze(ErrorInfo); /** @type {MakeCausalConsole} */ const makeCausalConsole = (baseConsole, loggedErrorHandler) => { + if (!baseConsole) { + return undefined; + } + const { getStackString, tagError, takeMessageLogArgs, takeNoteLogArgsArray } = loggedErrorHandler; diff --git a/packages/ses/src/error/internal-types.js b/packages/ses/src/error/internal-types.js index a62dcc329d..caca8cfbfe 100644 --- a/packages/ses/src/error/internal-types.js +++ b/packages/ses/src/error/internal-types.js @@ -83,7 +83,7 @@ * calls methods of the `loggedErrorHandler` to customize how it handles logged * errors. * - * @param {VirtualConsole} baseConsole + * @param {VirtualConsole | undefined} baseConsole * @param {LoggedErrorHandler} loggedErrorHandler - * @returns {VirtualConsole} + * @returns {VirtualConsole | undefined} */ diff --git a/packages/ses/src/error/tame-console.js b/packages/ses/src/error/tame-console.js index 3f3fca1a4b..5e81cb61ef 100644 --- a/packages/ses/src/error/tame-console.js +++ b/packages/ses/src/error/tame-console.js @@ -8,7 +8,11 @@ import './types.js'; import './internal-types.js'; // eslint-disable-next-line no-restricted-globals -const originalConsole = console; +const originalConsole = /** @type {VirtualConsole} */ ( + typeof console !== 'undefined' + ? console + : undefined +); /** * Wrap console unless suppressed. @@ -40,10 +44,11 @@ export const tameConsole = ( getStackString: optGetStackString, }; } - const ourConsole = + const ourConsole = /** @type {VirtualConsole} */ ( consoleTaming === 'unsafe' ? originalConsole - : makeCausalConsole(originalConsole, loggedErrorHandler); + : makeCausalConsole(originalConsole, loggedErrorHandler) + ); // Attach platform-specific error traps such that any error that gets thrown // at top-of-turn (the bottom of stack) will get logged by our causal