Skip to content

Commit

Permalink
Merge pull request #14 from hzrd149/x-reason
Browse files Browse the repository at this point in the history
Return X-Reason header for all errors
  • Loading branch information
hzrd149 authored Nov 7, 2024
2 parents c267585 + ad7341c commit 44345c3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-papayas-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"blossom-server-ts": minor
---

Return X-Reason header for all errors
2 changes: 1 addition & 1 deletion public/upload-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions src/api/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommonState>("/upload", async (ctx, next) => {
if (!config.upload.enabled) throw new HttpErrors.NotFound("Uploads disabled");

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 44345c3

Please sign in to comment.