From 93c770264c36ceb3767c5a8e7d2d268c1500e0eb Mon Sep 17 00:00:00 2001 From: Sebastien DUMETZ Date: Thu, 11 Jul 2024 09:11:22 +0200 Subject: [PATCH] improve 404 errors output --- source/server/routes/index.ts | 19 +++++++++++++++++++ source/server/templates/error.hbs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/source/server/routes/index.ts b/source/server/routes/index.ts index edd655c0..03839db9 100644 --- a/source/server/routes/index.ts +++ b/source/server/routes/index.ts @@ -240,6 +240,25 @@ export default async function createServer(config = defaultConfig) :Promise{ + //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) => { diff --git a/source/server/templates/error.hbs b/source/server/templates/error.hbs index 42a17b47..2108120e 100644 --- a/source/server/templates/error.hbs +++ b/source/server/templates/error.hbs @@ -11,7 +11,7 @@

{{message}}

-
{{stack}}
+
{{#if reason}}{{reason}}{{else}}{{stack}}{{/if}}
{{/with}}