···11+import { expect, test } from "bun:test";
22+33+import { api } from "./index";
44+55+test("Middleware error should return json", async () => {
66+ const res = await api.request("/status_report/1", {});
77+88+ const json = await res.json();
99+ expect(res.status).toBe(401);
1010+ expect(json).toMatchObject({
1111+ code: "UNAUTHORIZED",
1212+ message: "Unauthorized",
1313+ docs: "https://docs.openstatus.dev/api-references/errors/code/UNAUTHORIZED",
1414+ });
1515+});
+8-6
apps/server/src/v1/middleware.ts
···55import { workspace } from "@openstatus/db/src/schema";
66import { getPlanConfig } from "@openstatus/plans";
77import type { Variables } from "./index";
88+import { HTTPException } from "hono/http-exception";
89910export async function middleware(
1011 c: Context<{ Variables: Variables }, "/*">,
1111- next: Next,
1212+ next: Next
1213) {
1314 const key = c.req.header("x-openstatus-key");
1414- if (!key) return c.text("Unauthorized", 401);
1515+ if (!key) throw new HTTPException(401, { message: "Unauthorized" });
15161617 const { error, result } =
1718 process.env.NODE_ENV === "production"
1819 ? await verifyKey(key)
1920 : { result: { valid: true, ownerId: "1" }, error: null };
20212121- if (error) return c.text("Internal Server Error", 500);
2222- if (!result.valid) return c.text("Unauthorized", 401);
2323- if (!result.ownerId) return c.text("Unauthorized", 401);
2222+ if (error) throw new HTTPException(500, { message: error.message });
2323+ if (!result.valid) throw new HTTPException(401, { message: "Unauthorized" });
2424+ if (!result.ownerId)
2525+ throw new HTTPException(401, { message: "Unauthorized" });
24262527 const _workspace = await db
2628 .select()
···30323133 if (!_workspace) {
3234 console.error("Workspace not found");
3333- return c.text("Unauthorized", 401);
3535+ throw new HTTPException(401, { message: "Unauthorized" });
3436 }
35373638 c.set("workspacePlan", getPlanConfig(_workspace.plan));
+1-1
apps/server/src/v1/monitors/delete.ts
···56565757 // FIXME: Remove all relations of the monitor from all notifications, pages,....
58585959- return c.json({});
5959+ return c.json({}, 200);
6060 });
6161}