From ad7341cabd2633c30f3d214dcf2ecbfc2ad56768 Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Sun, 3 Nov 2024 21:24:01 +0000 Subject: [PATCH] Return X-Reason header for all errors --- .changeset/tough-papayas-join.md | 5 +++++ public/upload-form.js | 2 +- src/api/upload.ts | 18 ------------------ src/index.ts | 2 ++ 4 files changed, 8 insertions(+), 19 deletions(-) create mode 100644 .changeset/tough-papayas-join.md diff --git a/.changeset/tough-papayas-join.md b/.changeset/tough-papayas-join.md new file mode 100644 index 0000000..d561642 --- /dev/null +++ b/.changeset/tough-papayas-join.md @@ -0,0 +1,5 @@ +--- +"blossom-server-ts": minor +--- + +Return X-Reason header for all errors diff --git a/public/upload-form.js b/public/upload-form.js index dcdd7f4..b4dbe44 100644 --- a/public/upload-form.js +++ b/public/upload-form.js @@ -59,7 +59,7 @@ export class UploadForm extends LitElement { }); if (!check.ok) { - throw new Error(check.headers.get("x-upload-message") || "Upload Rejected"); + throw new Error(check.headers.get("x-reason") || "Upload Rejected"); } // Upload blob diff --git a/src/api/upload.ts b/src/api/upload.ts index eec8be5..9b42cf6 100644 --- a/src/api/upload.ts +++ b/src/api/upload.ts @@ -17,24 +17,6 @@ type UploadState = CommonState & { rule: Rule; }; -// handle errors -router.use(async (ctx, next) => { - try { - await next(); - } catch (err) { - // BUD-06 set `X-Upload-Message` on failure - if (isHttpError(err)) { - const status = (ctx.status = err.status || 500); - ctx.set("X-Upload-Message", status > 500 ? "Something went wrong" : err.message); - } else { - ctx.set("X-Upload-Message", "Something went wrong"); - } - - // pass error to parent handler - throw err; - } -}); - router.all("/upload", async (ctx, next) => { if (!config.upload.enabled) throw new HttpErrors.NotFound("Uploads disabled"); diff --git a/src/index.ts b/src/index.ts index 9a083ad..36d100d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,7 +41,9 @@ app.use(async (ctx, next) => { if (isHttpError(err)) { const status = (ctx.status = err.status || 500); if (status >= 500) console.error(err.stack); + // TODO: remove this when clients no long rely on it ctx.body = status > 500 ? { message: "Something went wrong" } : { message: err.message }; + ctx.set("X-Reason", status > 500 ? "Something went wrong" : err.message); } else { console.log(err); ctx.status = 500;