feat(api): standardize errors as problem details

This commit is contained in:
dmg
2026-08-01 14:42:19 -04:00
parent 40abab7abc
commit 2657399628
16 changed files with 441 additions and 17 deletions
+29
View File
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest";
import { problemInstance, problemResponse } from "./problem-response";
describe("problemResponse", () => {
it("uses the request path and query as the problem instance", () => {
expect(problemInstance(new Request("https://example.com/api/items?cursor=next")))
.toBe("/api/items?cursor=next");
});
it("returns RFC 9457 JSON with the required media type", async () => {
const response = problemResponse({
type: "urn:error:unauthorized",
title: "Unauthorized",
status: 401,
detail: "A valid Velocity server credential is required.",
instance: "/api/velocity/access",
});
expect(response.status).toBe(401);
expect(response.headers.get("content-type")).toBe("application/problem+json");
expect(await response.json()).toEqual({
type: "urn:error:unauthorized",
title: "Unauthorized",
status: 401,
detail: "A valid Velocity server credential is required.",
instance: "/api/velocity/access",
});
});
});