From 5ab08705a4738aa0d48e2148d34a9437f8403c6f Mon Sep 17 00:00:00 2001 From: dabico Date: Wed, 3 Apr 2024 18:01:56 +0200 Subject: [PATCH] Expose exception message or stack trace in global error handler --- index.js | 7 ++++++- views/error.ejs | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 748008a..56c3e73 100644 --- a/index.js +++ b/index.js @@ -102,7 +102,12 @@ app.use(actuator({ basePath: "/actuator" })); app.use("/", await router()); app.use(async (err, _req, res, _next) => { - res.status(HTTPStatus.INTERNAL_SERVER_ERROR).render("error", { message: err.message }); + const message = nodeEnv === "production" + ? err.message + : err.stack.split("\n") + .map(line => line.trimStart()) + .join("\n"); + res.status(HTTPStatus.INTERNAL_SERVER_ERROR).render("error", { message }); }); const server = app.listen(port, () => { diff --git a/views/error.ejs b/views/error.ejs index 25d2992..965c26b 100644 --- a/views/error.ejs +++ b/views/error.ejs @@ -13,13 +13,18 @@ <%= locals.title || "An Unexpected Error Has Occurred!" %> <% if (locals.message) { %> -

- <%= locals.message %> -

+
+
+
+
<%= locals.message %>
+
+
+
<% } %> <%- include('./partials/home-button'); %> <%- include('./partials/footer'); %> <%- include('./partials/scripts/general'); %> + <%- include('./partials/scripts/hljs'); %> \ No newline at end of file