From dacce643fecf04b9139b0f6977c686b2f42775a4 Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Thu, 27 Jul 2023 01:28:02 -0700 Subject: [PATCH] fix: :lock: Show 404 error on files not found --- packages/backend/src/server/web/index.ts | 48 ++++++++++++++++++------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/server/web/index.ts b/packages/backend/src/server/web/index.ts index a2ccf872fc..7f28f443f5 100644 --- a/packages/backend/src/server/web/index.ts +++ b/packages/backend/src/server/web/index.ts @@ -114,24 +114,48 @@ const router = new Router(); //#region static assets router.get("/static-assets/(.*)", async (ctx) => { - await send(ctx as any, ctx.path.replace("/static-assets/", ""), { - root: staticAssets, - maxage: 7 * DAY, - }); + try { + await send(ctx as any, ctx.path.replace("/static-assets/", ""), { + root: staticAssets, + maxage: 7 * DAY, + }); + } catch (e) { + if (e.status === 404) { + ctx.throw(404, "File not found"); + } else { + throw e; + } + } }); router.get("/client-assets/(.*)", async (ctx) => { - await send(ctx as any, ctx.path.replace("/client-assets/", ""), { - root: clientAssets, - maxage: 7 * DAY, - }); + try { + await send(ctx as any, ctx.path.replace("/client-assets/", ""), { + root: clientAssets, + maxage: 7 * DAY, + }); + } catch (e) { + if (e.status === 404) { + ctx.throw(404, "File not found"); + } else { + throw e; + } + } }); router.get("/assets/(.*)", async (ctx) => { - await send(ctx as any, ctx.path.replace("/assets/", ""), { - root: assets, - maxage: 7 * DAY, - }); + try { + await send(ctx as any, ctx.path.replace("/assets/", ""), { + root: assets, + maxage: 7 * DAY, + }); + } catch (e) { + if (e.status === 404) { + ctx.throw(404, "File not found"); + } else { + throw e; + } + } }); // Apple touch icon