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(" ");
}
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" });