| 1 | package provider |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // AuthError reports that a provider rejected the API key (HTTP 401/403). |
| 6 | // Itsmessageisalreadyuser-facingandactionable — it names the provider and, |
| 7 | // when known, the environment variable the key comes from — and it carries theserver's own reason as Body, |
| 8 | // because relay gateways explain *why* the key wasrejected ("token expired", key not entitled to the model) |
| 9 | // in the responsebody. Body is deliberately NOTpart of Error(): servers echo maskedkeyfragmentsinauthbodies, |
| 10 | // and the ambient error string flows intologs, |
| 11 | // status lines, and traces where key material must never propagate. Displaylayers that want the reason read |
| 12 | // Body and extract it themselves. Providersshould return this (rather than a generic status error) |
| 13 | // forauthfailures. |
| 14 | type AuthError struct { |
| 15 | ModelRef string // actual request model, assigned at the shared request boundary |
| 16 | Provider string // stable provider instance id, e.g. "deepseek" |
| 17 | ProviderDisplayName string // user-editable display label |
| 18 | Protocol string // configured wire adapter id |
| 19 | KeyEnv string // the api_key_env the key is read from, when known |
| 20 | KeySource string // human-readable source of KeyEnv, when known |
| 21 | Status int // the HTTP status (401 or 403) |
| 22 | HasKey bool // a non-empty key was sent — the server rejected it, vs. no key configured at all |
| 23 | Body string // trimmed response-body snippet, the server's verbatim reason when it gave one |
| 24 | } |
| 25 | |
| 26 | func (e *AuthError) Error() string { |
| 27 | key := "the API key" |
| 28 | if e.KeyEnv != "" { |
| 29 | key = e.KeyEnv |
| 30 | } |
| 31 | if e.KeySource != "" { |
| 32 | key += " from " + e.KeySource |
| 33 | } |
| 34 | return fmt.Sprintf("authentication failed for provider %q (HTTP %d): %s is invalid or expired — update it (in .env or your environment) and retry, or run `reasonix setup`", |
| 35 | ProviderDisplayLabel(e.Provider, e.ProviderDisplayName, e.Protocol), e.Status, key) |
| 36 | } |
| 37 |