Files
cfw-attachment/src/problem.ts
2026-07-02 07:34:48 -07:00

34 lines
770 B
TypeScript

export class HttpProblem extends Error {
constructor(
readonly status: number,
readonly code: string,
message: string,
readonly detail: Record<string, unknown> = {},
) {
super(message);
}
}
export function problemResponse(error: unknown): Response {
const problem =
error instanceof HttpProblem
? error
: new HttpProblem(500, "internal-error", "Internal server error");
return Response.json(
{
type: `https://cfw-attachment.bowong.cc/problems/${problem.code}`,
title: problem.message,
status: problem.status,
detail: problem.detail,
code: problem.code,
},
{
status: problem.status,
headers: {
"content-type": "application/problem+json",
},
},
);
}