From e45c8901192351b27f1dc57ac34f11815a088651 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 2 Aug 2026 22:31:59 -0400 Subject: [PATCH] refactor: extract formatError/logError from repeated catch blocks The `e.stack ? \`${e}\n\n${e.stack}\` : \`${e}\`` + createLog(...true) pattern appeared in four catch blocks. Extract formatError(e) and logError(structureId, routeId, e) and call them instead. Behavior and the 500 response body are unchanged. Co-Authored-By: Claude Opus 4.8 --- index.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 9620d69..7410361 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,15 @@ function inspectArgs(args) { return args.map((v) => util.inspect(v, INSPECT_OPTS)).join(" "); } +function formatError(e) { + return e.stack ? `${e}\n\n${e.stack}` : `${e}`; +} + +// Record a handler error in the structure's logs table. +function logError(structureId, routeId, e) { + model.createLog(structureId, routeId, formatError(e), true); +} + app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(express.static("public")); @@ -162,8 +171,7 @@ function bootstrapWebsocketHandler(route) { cb(...args) } catch (e) { - const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; - model.createLog(route.structure_id, route.id, error, true); + logError(route.structure_id, route.id, e); } }) } @@ -189,14 +197,12 @@ function bootstrapWebsocketHandler(route) { try { return handler(ws, req) } catch (e) { - const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; - model.createLog(route.structure_id, route.id, error, true); + logError(route.structure_id, route.id, e); } } model.updateRoute({ ...route, error: null }); } catch (e) { - const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; - model.createLog(route.structure_id, route.id, error, true); + logError(route.structure_id, route.id, e); model.updateRoute({ ...route, error: e.stack }); } } @@ -840,9 +846,8 @@ app.all("*", async (req, res) => { await handlerScript.runInContext(context); const result = await executionScript.runInContext(context); } catch (e) { - const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; - model.createLog(structure.id, route.id, error, true); - return res.status(500).send(error); + logError(structure.id, route.id, e); + return res.status(500).send(formatError(e)); } } else { res.status(404).json({ success: false, message: "Path not found" });