export class HttpProblem extends Error { constructor( readonly status: number, readonly code: string, message: string, readonly detail: Record = {}, ) { 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", }, }, ); }