Spaces:
Paused
Paused
Download console/src/api/error.ts from mysticaldragon/HF: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/mysticaldragon/HF/resolve/main/console/src/api/error.ts
- Command line
-
hf download hf://spaces/mysticaldragon/HF/console/src/api/error.ts
-
curl -L -o error.ts https://huggingface.co/spaces/mysticaldragon/HF/resolve/main/console/src/api/error.ts
1.48 kB
| interface ValidationIssue { | |
| loc?: unknown; | |
| msg?: unknown; | |
| } | |
| function validationIssueMessage(issue: ValidationIssue): string | null { | |
| if (typeof issue.msg !== "string" || !issue.msg) return null; | |
| if (!Array.isArray(issue.loc)) return issue.msg; | |
| const path = issue.loc | |
| .filter((part) => part !== "body") | |
| .filter((part): part is string | number => | |
| ["string", "number"].includes(typeof part), | |
| ) | |
| .join("."); | |
| return path ? `${path}: ${issue.msg}` : issue.msg; | |
| } | |
| export function formatApiError(payload: unknown, fallback: string): string { | |
| if (!payload || typeof payload !== "object") return fallback; | |
| const detail = (payload as { detail?: unknown }).detail; | |
| if (typeof detail === "string" && detail) return detail; | |
| if (Array.isArray(detail)) { | |
| const messages = detail | |
| .map((issue) => | |
| issue && typeof issue === "object" | |
| ? validationIssueMessage(issue as ValidationIssue) | |
| : null, | |
| ) | |
| .filter((message): message is string => Boolean(message)); | |
| if (messages.length) return messages.join("; "); | |
| } | |
| if (detail && typeof detail === "object") { | |
| const message = validationIssueMessage(detail as ValidationIssue); | |
| if (message) return message; | |
| } | |
| return fallback; | |
| } | |
| export async function responseErrorMessage( | |
| response: Response, | |
| fallback: string, | |
| ): Promise<string> { | |
| const payload: unknown = await response.json().catch(() => null); | |
| return formatApiError(payload, fallback); | |
| } | |