| 250 | }; |
| 251 | |
| 252 | export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => { |
| 253 | const errors: Record<number, string> = { |
| 254 | 400: 'Bad Request', |
| 255 | 401: 'Unauthorized', |
| 256 | 403: 'Forbidden', |
| 257 | 404: 'Not Found', |
| 258 | 500: 'Internal Server Error', |
| 259 | 502: 'Bad Gateway', |
| 260 | 503: 'Service Unavailable', |
| 261 | ...options.errors, |
| 262 | } |
| 263 | |
| 264 | const error = errors[result.status]; |
| 265 | if (error) { |
| 266 | throw new ApiError(options, result, error); |
| 267 | } |
| 268 | |
| 269 | if (!result.ok) { |
| 270 | const errorStatus = result.status ?? 'unknown'; |
| 271 | const errorStatusText = result.statusText ?? 'unknown'; |
| 272 | const errorBody = (() => { |
| 273 | try { |
| 274 | return JSON.stringify(result.body, null, 2); |
| 275 | } catch (e) { |
| 276 | return undefined; |
| 277 | } |
| 278 | })(); |
| 279 | |
| 280 | throw new ApiError(options, result, |
| 281 | `Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}` |
| 282 | ); |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | /** |
| 287 | * Request method |