From ac69dee38736adcca84001b0b1836feea334b4f9 Mon Sep 17 00:00:00 2001 From: leukipp Date: Sun, 21 Sep 2025 22:47:02 +0200 Subject: [PATCH] ref: improve error handling --- js/hardware.js | 22 +++++++++------------- js/integration.js | 4 ++-- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/js/hardware.js b/js/hardware.js index e927d9d..8cd9c64 100644 --- a/js/hardware.js +++ b/js/hardware.js @@ -746,7 +746,7 @@ const execSyncCommand = (cmd, args = []) => { const output = cpr.execSync([cmd, ...args].join(" "), { encoding: "utf8" }); return output.trim().replace(/\0/g, ""); } catch (error) { - console.error("Execute Sync:", error.message); + console.error(`Execute Sync: '${[cmd, ...args].join(" ")}' --> ${error.message}`); } return null; }; @@ -774,17 +774,13 @@ const execAsyncCommand = (cmd, args = [], callback = null) => { } }); proc.on("close", (code) => { - try { - if (typeof callback === "function") { - if (code === 0) { - callback(successOutput.trim().replace(/\0/g, ""), null); - } else { - callback(null, errorOutput.trim().replace(/\0/g, "")); - } - } - } catch (error) { - console.error("Execute Async:", error.message); - if (typeof callback === "function") callback(null, error.message); + const error = errorOutput.trim().replace(/\0/g, ""); + const reply = successOutput.trim().replace(/\0/g, ""); + if (code !== 0 || error) { + console.error(`Execute Async: '${[cmd, ...args].join(" ")}' --> ${error} (${code})`); + if (typeof callback === "function") callback(null, error); + } else { + if (typeof callback === "function") callback(reply, null); } }); return proc; @@ -830,7 +826,7 @@ const execScriptCommand = (cmd, args = [], callback = null) => { }); proc.on("close", (code) => { if (code !== 0) { - console.log(`Script exited with error code ${code}.`); + console.error(`Script exited with error code (${code}).`); if (typeof callback === "function") callback(null, code); } else { console.log("Script exited successfully."); diff --git a/js/integration.js b/js/integration.js index f7ed460..b82eee3 100644 --- a/js/integration.js +++ b/js/integration.js @@ -432,7 +432,7 @@ const initDisplay = () => { if (!error) { hardware.update(); } else { - console.warn("Failed:", error); + console.warn("Command Failed:", error); } }); } else if (topic === config.brightness_command_topic) { @@ -442,7 +442,7 @@ const initDisplay = () => { if (!error) { hardware.update(); } else { - console.warn("Failed:", error); + console.warn("Command Failed:", error); } }); }