Skip to content

Commit

Permalink
improve 404 errors output
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Jul 11, 2024
1 parent d60e659 commit addade2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions source/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,25 @@ export default async function createServer(config = defaultConfig) :Promise<expr
const isTTY = process.stderr.isTTY;

// error handling
//This should be last as it will match everything
app.use((req, res)=>{
//We don't just throw an error to be able to differentiate between
//internally-thrown 404 and routes that doesn't exist in logs
const err = { code:404, message: `Not Found`, reason: `No route was defined that could match ${req.originalUrl}`}
res.format({
"application/json": ()=> {
res.status(404).send(err)
},
"text/html": ()=>{
res.status(404).render("error", { err });
},
"text/plain": ()=>{
res.status(404).send(err.message);
},
default: ()=> res.status(404).send(err.message),
});
});

// istanbul ignore next
//@ts-ignore
app.use((error, req, res, next) => {
Expand Down
2 changes: 1 addition & 1 deletion source/server/templates/error.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<p class="message">{{message}}</p>

<pre class="stack"><code>{{stack}}</code></pre>
<pre class="stack"><code>{{#if reason}}{{reason}}{{else}}{{stack}}{{/if}}</code></pre>

{{/with}}
</main>
Expand Down

0 comments on commit addade2

Please sign in to comment.