|
1 | 1 |
|
| 2 | +type HttpErrorDetails = { |
| 3 | + status: number | null; |
| 4 | + headers?: unknown; |
| 5 | +}; |
| 6 | + |
2 | 7 | /** |
3 | | - * Extract an HTTP status code from a thrown error across the libraries used by |
4 | | - * the code-host clients: |
5 | | - * - Octokit RequestError: { status } |
6 | | - * - openapi-fetch (Bitbucket Cloud / Server): direct throws with { status } |
7 | | - * or errors wrapped via Object.assign(new Error(...), { status }) |
8 | | - * - gitbeaker (GitLab): { cause: { response: { status } } } |
| 8 | + * Normalizes HTTP response metadata across the code-host client libraries: |
| 9 | + * - Octokit RequestError: { status, response: { headers } } |
| 10 | + * - openapi-fetch (Bitbucket Cloud / Server): { status } |
| 11 | + * - gitbeaker (GitLab): { cause: { response: { status, headers } } } |
9 | 12 | */ |
10 | | -export const getErrorStatus = (err: unknown): number | null => { |
11 | | - if (err === null || typeof err !== 'object') { |
12 | | - return null; |
| 13 | +const getHttpErrorDetails = (error: unknown): HttpErrorDetails => { |
| 14 | + if (error === null || typeof error !== 'object') { |
| 15 | + return { status: null }; |
13 | 16 | } |
14 | 17 |
|
15 | | - const direct = (err as { status?: unknown }).status; |
16 | | - if (typeof direct === 'number') { |
17 | | - return direct; |
| 18 | + const directError = error as { |
| 19 | + status?: unknown; |
| 20 | + response?: { status?: unknown; headers?: unknown }; |
| 21 | + cause?: { response?: { status?: unknown; headers?: unknown } }; |
| 22 | + }; |
| 23 | + const directResponse = directError.response; |
| 24 | + const nestedResponse = directError.cause?.response; |
| 25 | + const status = [ |
| 26 | + directError.status, |
| 27 | + directResponse?.status, |
| 28 | + nestedResponse?.status, |
| 29 | + ].find((value): value is number => typeof value === 'number') ?? null; |
| 30 | + |
| 31 | + return { |
| 32 | + status, |
| 33 | + headers: directResponse?.headers ?? nestedResponse?.headers, |
| 34 | + }; |
| 35 | +}; |
| 36 | + |
| 37 | +export const getErrorStatus = (error: unknown): number | null => |
| 38 | + getHttpErrorDetails(error).status; |
| 39 | + |
| 40 | +export const getErrorHeader = (error: unknown, name: string): string | undefined => { |
| 41 | + const { headers } = getHttpErrorDetails(error); |
| 42 | + |
| 43 | + if (headers instanceof Headers) { |
| 44 | + return headers.get(name) ?? undefined; |
18 | 45 | } |
19 | 46 |
|
20 | | - const nested = (err as { cause?: { response?: { status?: unknown } } }).cause?.response?.status; |
21 | | - if (typeof nested === 'number') { |
22 | | - return nested; |
| 47 | + if (headers !== null && typeof headers === 'object') { |
| 48 | + const normalizedName = name.toLowerCase(); |
| 49 | + const entry = Object.entries(headers).find(([key]) => key.toLowerCase() === normalizedName); |
| 50 | + return typeof entry?.[1] === 'string' ? entry[1] : undefined; |
23 | 51 | } |
24 | 52 |
|
25 | | - return null; |
| 53 | + return undefined; |
26 | 54 | }; |
27 | 55 |
|
28 | 56 | export const isUnauthorized = (err: unknown): boolean => getErrorStatus(err) === 401; |
|
0 commit comments