| |
|
|
| import { z } from 'zod' |
| import * as schemas from './schemas.js' |
|
|
| |
| |
| |
| function parseRetryAfter(header: string | null): number | undefined { |
| if (header === null) { |
| return undefined |
| } |
| const trimmed = header.trim() |
| return /^\d+$/.test(trimmed) ? Number(trimmed) : undefined |
| } |
|
|
| export class HTTPError extends Error { |
| constructor( |
| message: string, |
| public type: string, |
| public title: string, |
| public status: number, |
| public url: string, |
| public retryAfter: number | undefined, |
| protected __raw?: Record<string, unknown>, |
| ) { |
| super(message) |
| this.name = 'HTTPError' |
| } |
|
|
| static fromResponse(resp: { |
| response: Response |
| error?: z.infer<typeof schemas.baseError> |
| }) { |
| const retryAfter = parseRetryAfter(resp.response.headers.get('Retry-After')) |
|
|
| if ( |
| resp.response.headers |
| .get('Content-Type') |
| ?.includes('application/problem+json') && |
| resp.error |
| ) { |
| return new HTTPError( |
| resp.error.detail, |
| resp.error.type ?? resp.error.title, |
| resp.error.title, |
| resp.error.status ?? resp.response.status, |
| resp.response.url, |
| retryAfter, |
| resp.error, |
| ) |
| } |
|
|
| return new HTTPError( |
| `Request failed: ${resp.response.statusText}`, |
| resp.response.statusText, |
| resp.response.statusText, |
| resp.response.status, |
| resp.response.url, |
| retryAfter, |
| ) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| get invalidParameters(): |
| | z.infer<typeof schemas.invalidParameters> |
| | undefined { |
| return this.__raw?.invalid_parameters as |
| | z.infer<typeof schemas.invalidParameters> |
| | undefined |
| } |
|
|
| |
| |
| |
| |
| getField(key: string) { |
| return this.__raw?.[key] |
| } |
| } |
|
|