From 6ec4cc7c8906ebbe7eafeec071b5331281eb6ace Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 10 Apr 2024 13:11:10 +0530 Subject: [PATCH] Log unhandled errors and promise rejections in the desktop app Test code: setTimeout(() => testFunctionSync(), 5000); setTimeout( () => testFunction().then(() => console.log("done testFunction")), 10000, ); const testFunctionSync = () => { console.log("sleeping not"); throw new Error("Handle me"); }; const testFunction = async () => { console.log("sleeping"); await fs.mkdir("/tmp/foo", { recursive: true }); console.log("woke up"); throw new Error("Handle me"); }; --- desktop/src/main/log.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/desktop/src/main/log.ts b/desktop/src/main/log.ts index 04ecb6ea3b..d43161feaf 100644 --- a/desktop/src/main/log.ts +++ b/desktop/src/main/log.ts @@ -19,6 +19,16 @@ export const initLogging = () => { log.transports.file.format = "[{y}-{m}-{d}T{h}:{i}:{s}{z}] {text}"; log.transports.console.level = false; + + // Log unhandled errors and promise rejections. + log.errorHandler.startCatching({ + onError: ({ error, errorName }) => { + logError(errorName, error); + // Prevent the default electron-log actions (e.g. showing a dialog) + // from getting triggered. + return false; + }, + }); }; /**