* Turn a fetch failure into something a human can act on. "source fetch * failed" alone is useless when the real problem is a Docker container that * can't resolve a sibling's hostname or an API rate limit (#24, #32).
(e: unknown)
| 65 | * can't resolve a sibling's hostname or an API rate limit (#24, #32). |
| 66 | */ |
| 67 | function describeFetchError(e: unknown): string { |
| 68 | if (!(e instanceof Error)) return "fetch failed"; |
| 69 | if (e.name === "TimeoutError" || e.name === "AbortError") return "timeout after 5s"; |
| 70 | const code: string | undefined = |
| 71 | (e.cause as { code?: string } | undefined)?.code ?? (e as { code?: string }).code; |
| 72 | switch (code) { |
| 73 | case "ENOTFOUND": |
| 74 | case "EAI_AGAIN": |
| 75 | return "DNS lookup failed"; |
| 76 | case "ECONNREFUSED": |
| 77 | return "connection refused"; |
| 78 | case "EHOSTUNREACH": |
| 79 | case "ENETUNREACH": |
| 80 | return "host unreachable"; |
| 81 | case "ETIMEDOUT": |
| 82 | return "connect timeout"; |
| 83 | case "ECONNRESET": |
| 84 | return "connection reset"; |
| 85 | } |
| 86 | if (code) return code; |
| 87 | return e.message || "fetch failed"; |
| 88 | } |
| 89 | |
| 90 | /** Hold off the API after an HTTP 429 — hammering through a rate limit just |
| 91 | * extends it (the freeze/vanish/reappear cycle in #24). */ |