(providerID: ProviderV2.ID, e: APICallError)
| 30 | // Providers not reliably handled in this function: |
| 31 | // - z.ai: can accept overflow silently (needs token-count/context-window checks) |
| 32 | function message(providerID: ProviderV2.ID, e: APICallError) { |
| 33 | return iife(() => { |
| 34 | const msg = e.message |
| 35 | if (msg === "") { |
| 36 | if (e.responseBody) return e.responseBody |
| 37 | if (e.statusCode) { |
| 38 | const err = STATUS_CODES[e.statusCode] |
| 39 | if (err) return err |
| 40 | } |
| 41 | return "Unknown error" |
| 42 | } |
| 43 | |
| 44 | if (!e.responseBody || (e.statusCode && msg !== STATUS_CODES[e.statusCode])) { |
| 45 | return msg |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | const body = JSON.parse(e.responseBody) |
| 50 | // try to extract common error message fields |
| 51 | const errMsg = body.message || body.error || body.error?.message |
| 52 | if (errMsg && typeof errMsg === "string") { |
| 53 | return `${msg}: ${errMsg}` |
| 54 | } |
| 55 | } catch {} |
| 56 | |
| 57 | // If responseBody is HTML (e.g. from a gateway or proxy error page), |
| 58 | // provide a human-readable message instead of dumping raw markup |
| 59 | if (/^\s*<!doctype|^\s*<html/i.test(e.responseBody)) { |
| 60 | if (e.statusCode === 401) { |
| 61 | return "Unauthorized: request was blocked by a gateway or proxy. Your authentication token may be missing or expired — try running `opencode auth login <your provider URL>` to re-authenticate." |
| 62 | } |
| 63 | if (e.statusCode === 403) { |
| 64 | return "Forbidden: request was blocked by a gateway or proxy. You may not have permission to access this resource — check your account and provider settings." |
| 65 | } |
| 66 | return msg |
| 67 | } |
| 68 | |
| 69 | return `${msg}: ${e.responseBody}` |
| 70 | }).trim() |
| 71 | } |
| 72 | |
| 73 | function json(input: unknown) { |
| 74 | if (typeof input === "string") { |
no test coverage detected