From 7d2b5128e456f8524b40638fbd1fbd758f94cb3c Mon Sep 17 00:00:00 2001 From: Oskar Nyberg Date: Thu, 19 Oct 2023 14:00:51 +0200 Subject: [PATCH] Add console.log error handling --- CHANGELOG.md | 1 + gui/src/shared/logging.ts | 66 +++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8889750a2316..386899ac42c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ Line wrap the file at 100 chars. Th ### Fixed - Show correct endpoint in CLI for custom relays. - Lower risk of being rate limited. +- Fix error dialog when failing to write to console by handling the thrown error. #### Windows - Correctly detect whether OS is Windows Server (primarily for logging in daemon.log). diff --git a/gui/src/shared/logging.ts b/gui/src/shared/logging.ts index c2537e00d419..0be8de8c4b80 100644 --- a/gui/src/shared/logging.ts +++ b/gui/src/shared/logging.ts @@ -55,43 +55,55 @@ export class Logger { this.outputs .filter((output) => level <= output.level) .forEach(async (output) => { - const maybePromise = output.write(level, message); - if (maybePromise instanceof Promise) { - try { - await maybePromise; - } catch (e) { - const error = e as Error; - console.error( - `${output.constructor.name}.write: ${error.message}. Original message: ${message}`, - ); - } + try { + await output.write(level, message); + } catch (e) { + const error = e as Error; + console.error( + `${output.constructor.name}.write: ${error.message}. Original message: ${message}`, + ); } }); } } export class ConsoleOutput implements ILogOutput { + private disabled = false; + constructor(public level: LogLevel) {} public write(level: LogLevel, message: string) { - switch (level) { - case LogLevel.error: - console.error(message); - break; - case LogLevel.warning: - console.warn(message); - break; - case LogLevel.info: - console.info(message); - break; - case LogLevel.verbose: - console.log(message); - break; - case LogLevel.debug: - console.log(message); - break; + if (this.disabled) { + return; + } + + try { + switch (level) { + case LogLevel.error: + console.error(message); + break; + case LogLevel.warning: + console.warn(message); + break; + case LogLevel.info: + console.info(message); + throw new Error('Log failed'); + break; + case LogLevel.verbose: + console.log(message); + break; + case LogLevel.debug: + console.log(message); + break; + } + } catch (error) { + this.disabled = true; + + const message = error instanceof Object && 'message' in error ? error.message : ''; + logger.error('Disabling console output due to:', message, error); } } } -export default new Logger(); +const logger = new Logger(); +export default logger;