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 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-08-02 22:31:59 -04:00
parent de94d43e48
commit e45c890119

View file

@ -33,6 +33,15 @@ function inspectArgs(args) {
return args.map((v) => util.inspect(v, INSPECT_OPTS)).join(" "); 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.urlencoded({ extended: true }));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(express.static("public")); app.use(express.static("public"));
@ -162,8 +171,7 @@ function bootstrapWebsocketHandler(route) {
cb(...args) cb(...args)
} }
catch (e) { catch (e) {
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; logError(route.structure_id, route.id, e);
model.createLog(route.structure_id, route.id, error, true);
} }
}) })
} }
@ -189,14 +197,12 @@ function bootstrapWebsocketHandler(route) {
try { try {
return handler(ws, req) return handler(ws, req)
} catch (e) { } catch (e) {
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; logError(route.structure_id, route.id, e);
model.createLog(route.structure_id, route.id, error, true);
} }
} }
model.updateRoute({ ...route, error: null }); model.updateRoute({ ...route, error: null });
} catch (e) { } catch (e) {
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; logError(route.structure_id, route.id, e);
model.createLog(route.structure_id, route.id, error, true);
model.updateRoute({ ...route, error: e.stack }); model.updateRoute({ ...route, error: e.stack });
} }
} }
@ -840,9 +846,8 @@ app.all("*", async (req, res) => {
await handlerScript.runInContext(context); await handlerScript.runInContext(context);
const result = await executionScript.runInContext(context); const result = await executionScript.runInContext(context);
} catch (e) { } catch (e) {
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; logError(structure.id, route.id, e);
model.createLog(structure.id, route.id, error, true); return res.status(500).send(formatError(e));
return res.status(500).send(error);
} }
} else { } else {
res.status(404).json({ success: false, message: "Path not found" }); res.status(404).json({ success: false, message: "Path not found" });