| 77 | } |
| 78 | |
| 79 | export async function classifyResponse(request: Request, response: Response): Promise<HttpClientError> { |
| 80 | let raw = '' |
| 81 | try { |
| 82 | raw = await response.clone().text() |
| 83 | } |
| 84 | catch { |
| 85 | // ignore read errors; raw stays '' |
| 86 | } |
| 87 | |
| 88 | const serverError = parseServerError(raw) |
| 89 | const status = response.status |
| 90 | const c = statusClass(status) |
| 91 | return new HttpClientError({ |
| 92 | code: c.code, |
| 93 | message: serverError?.message ?? c.fallbackMessage(status), |
| 94 | hint: c.hint, |
| 95 | httpStatus: status, |
| 96 | method: request.method, |
| 97 | url: redactBearer(response.url || request.url), |
| 98 | rawResponse: c.includeRaw && raw !== '' ? raw : undefined, |
| 99 | serverError, |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | export function classifyTransportError(err: unknown): BaseError { |
| 104 | if (err instanceof BaseError) { |