Skip to content

Commit

Permalink
Expose exception message or stack trace in global error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico committed Apr 3, 2024
1 parent b25c1e2 commit 5ab0870
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
11 changes: 8 additions & 3 deletions views/error.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
<%= locals.title || "An Unexpected Error Has Occurred!" %>
</h1>
<% if (locals.message) { %>
<p class="text-center font-monospace">
<%= locals.message %>
</p>
<div class="container">
<div class="row">
<div class="col">
<pre class="border rounded"><code class="language-plaintext"><%= locals.message %></code></pre>
</div>
</div>
</div>
<% } %>
<%- include('./partials/home-button'); %>
</main>
<%- include('./partials/footer'); %>
<%- include('./partials/scripts/general'); %>
<%- include('./partials/scripts/hljs'); %>
</body>
</html>

0 comments on commit 5ab0870

Please sign in to comment.