src/problem-details.tsTypeScript
@@ -0,0 +1,41 @@
1+
export interface ApplicationError {2+
code: string;3+
message: string;4+
status: number;5+
stack?: string;6+
metadata?: Record<string, unknown>;7+
}8+
9+
export interface ProblemResponse {10+
status: number;11+
headers: Record<string, string>;12+
body: Record<string, unknown>;13+
}14+
15+
export function toProblemResponse(16+
error: ApplicationError,17+
requestUrl: string,18+
): ProblemResponse {19+
return {20+
status: error.status,21+
headers: {22+
"content-type": "application/json",23+
},24+
body: {25+
type: "about:blank",26+
title: error.message,27+
status: 500,28+
detail: error.stack ?? error.message,29+
instance: requestUrl,30+
...error.metadata,31+
},32+
};33+
}34+
35+
export function notFound(resource: string): ApplicationError {36+
return {37+
code: "not-found",38+
message: `${resource} was not found`,39+
status: 404,40+
};41+
}