feat(api): standardize errors as problem details
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { GET } from "./route";
|
||||
|
||||
describe("unknown API routes", () => {
|
||||
it("return an RFC 9457 not-found problem", async () => {
|
||||
const response = GET(new Request("http://localhost/api/does-not-exist"));
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(response.headers.get("content-type")).toBe("application/problem+json");
|
||||
expect(await response.json()).toEqual({
|
||||
type: "urn:error:not-found",
|
||||
title: "API route not found",
|
||||
status: 404,
|
||||
detail: "The requested API route does not exist.",
|
||||
instance: "/api/does-not-exist",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { problemDetails } from "@minecraft-account-manager/contracts";
|
||||
import { problemInstance, problemResponse } from "@/lib/problem-response";
|
||||
|
||||
function notFound(request: Request) {
|
||||
const instance = problemInstance(request);
|
||||
return problemResponse(problemDetails(
|
||||
"urn:error:not-found",
|
||||
"API route not found",
|
||||
404,
|
||||
"The requested API route does not exist.",
|
||||
instance,
|
||||
));
|
||||
}
|
||||
|
||||
export const GET = notFound;
|
||||
export const POST = notFound;
|
||||
export const PUT = notFound;
|
||||
export const PATCH = notFound;
|
||||
export const DELETE = notFound;
|
||||
export const OPTIONS = notFound;
|
||||
Reference in New Issue
Block a user