Download src/utils/api-error-message.ts from SaylorTwift/openhands: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/SaylorTwift/openhands/resolve/main/src/utils/api-error-message.ts
- Command line
-
hf download hf://SaylorTwift/openhands/src/utils/api-error-message.ts
-
curl -L -o api-error-message.ts https://huggingface.co/SaylorTwift/openhands/resolve/main/src/utils/api-error-message.ts
1.27 kB
| import axios from "axios"; | |
| /** | |
| * Extract the parsed response body from a failed API call. | |
| * | |
| * Handles both transports the app uses: local agent-server calls that | |
| * throw an `AxiosError` (body under `error.response.data`) and cloud | |
| * calls through the shared TypeScript client that throw an `HttpError` | |
| * (parsed body directly under `error.response`). | |
| */ | |
| export function getApiErrorBody(error: unknown): unknown { | |
| if (axios.isAxiosError(error)) return error.response?.data; | |
| if (error instanceof Error && "response" in error) { | |
| return (error as { response?: unknown }).response; | |
| } | |
| return undefined; | |
| } | |
| /** | |
| * Extract a human-readable message from a failed API call. Prefers the | |
| * server-provided `message`/`detail` fields, then the `Error` message, | |
| * then `fallback`. | |
| */ | |
| export function getApiErrorMessage(error: unknown, fallback: string): string { | |
| const body = getApiErrorBody(error); | |
| if (body && typeof body === "object") { | |
| const { message, detail } = body as { | |
| message?: unknown; | |
| detail?: unknown; | |
| }; | |
| if (typeof message === "string" && message) return message; | |
| if (typeof detail === "string" && detail) return detail; | |
| } | |
| if (error instanceof Error && error.message) return error.message; | |
| return fallback; | |
| } | |