| | | 1 | | import type { components } from "~/api/types"; |
| | | 2 | | |
| | | 3 | | type ProblemDetails = components["schemas"]["Microsoft.AspNetCore.Mvc.ProblemDetails"]; |
| | | 4 | | type ValidationProblemDetails = |
| | | 5 | | components["schemas"]["Microsoft.AspNetCore.Http.HttpValidationProblemDetails"]; |
| | | 6 | | |
| | | 7 | | export type ProblemBody = ProblemDetails | ValidationProblemDetails; |
| | | 8 | | |
| | | 9 | | export class ApiProblemError extends Error { |
| | | 10 | | readonly status: number; |
| | | 11 | | readonly title: string; |
| | | 12 | | readonly detail?: string; |
| | | 13 | | readonly instance?: string; |
| | | 14 | | readonly type?: string; |
| | | 15 | | readonly errors?: Record<string, string[]>; |
| | | 16 | | readonly requestId?: string; |
| | | 17 | | readonly traceId?: string; |
| | | 18 | | readonly raw: ProblemBody; |
| | | 19 | | |
| | 18 | 20 | | constructor(status: number, body: ProblemBody) { |
| | 18 | 21 | | const title = body.title ?? `HTTP ${status}`; |
| | 18 | 22 | | super(body.detail ?? title); |
| | 18 | 23 | | this.name = "ApiProblemError"; |
| | 18 | 24 | | this.status = status; |
| | 18 | 25 | | this.title = title; |
| | 18 | 26 | | this.detail = body.detail ?? undefined; |
| | 18 | 27 | | this.instance = body.instance ?? undefined; |
| | 18 | 28 | | this.type = body.type ?? undefined; |
| | 18 | 29 | | this.errors = (body as ValidationProblemDetails).errors ?? undefined; |
| | 18 | 30 | | const ext = body as { requestId?: unknown; traceId?: unknown }; |
| | 18 | 31 | | this.requestId = typeof ext.requestId === "string" ? ext.requestId : undefined; |
| | 18 | 32 | | this.traceId = typeof ext.traceId === "string" ? ext.traceId : undefined; |
| | 18 | 33 | | this.raw = body; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | 18 | 37 | | export function isApiProblem(value: unknown): value is ApiProblemError { |
| | 18 | 38 | | return value instanceof ApiProblemError; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | interface RouteErrorResponseLike { |
| | | 42 | | status: number; |
| | | 43 | | statusText?: string; |
| | | 44 | | data?: unknown; |
| | | 45 | | } |
| | | 46 | | |
| | 6 | 47 | | function isRouteErrorResponseLike(value: unknown): value is RouteErrorResponseLike { |
| | 6 | 48 | | return ( |
| | | 49 | | value != null && |
| | | 50 | | typeof value === "object" && |
| | | 51 | | typeof (value as { status?: unknown }).status === "number" && |
| | | 52 | | "data" in value |
| | | 53 | | ); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /** |
| | | 57 | | * Coerce any thrown value into an ApiProblemError when possible, regardless of |
| | | 58 | | * how it survived (or did not survive) RR7's SSR serialization. Use this in |
| | | 59 | | * route ErrorBoundaries — the typed wrapper throws a `Response` for HTTP |
| | | 60 | | * failures, which RR7 hands back as a route ErrorResponse with `data` holding |
| | | 61 | | * the parsed ProblemBody. |
| | | 62 | | */ |
| | 7 | 63 | | export function asApiProblem(error: unknown): ApiProblemError | null { |
| | 7 | 64 | | if (isApiProblem(error)) return error; |
| | 6 | 65 | | if (isRouteErrorResponseLike(error)) { |
| | 3 | 66 | | const data = error.data; |
| | 3 | 67 | | let body: ProblemBody | null = null; |
| | 3 | 68 | | if (data && typeof data === "object") { |
| | 2 | 69 | | body = data as ProblemBody; |
| | 1 | 70 | | } else if (typeof data === "string" && data.length > 0) { |
| | 1 | 71 | | try { |
| | 1 | 72 | | body = JSON.parse(data) as ProblemBody; |
| | | 73 | | } catch { |
| | 0 | 74 | | body = null; |
| | | 75 | | } |
| | | 76 | | } |
| | 3 | 77 | | if (body) return new ApiProblemError(error.status, body); |
| | | 78 | | } |
| | 3 | 79 | | return null; |
| | | 80 | | } |