pub fn status_error(status: u16, message: impl Into<String>) -> ErrorExpand description
The error an HTTP error status indicates on its own, before the body is read.
| Status | Error |
|---|---|
| 3xx | Error::UnexpectedResponse, not retryable (a redirect, which is never followed: see redirect_error) |
| 401, 403 | Error::Auth |
| 402 | Error::QuotaExhausted |
| 408, 504, 524 | Error::Timeout |
| 429 | Error::RateLimited |
| 499 | Error::Provider, not retryable |
| other 4xx | Error::InvalidRequest |
| 503, 529 | Error::Overloaded |
| 501, 505 | Error::Provider, not retryable |
| anything else | Error::Provider, retryable when 5xx |
Every bundled HTTP provider falls back to it after its own error codes
and body, so one status means the same thing on every provider. Refine
it from what the body says: a quota code on a 429 is
Error::QuotaExhausted, a 404 that names the model is
Error::ModelNotFound (a bare 404 may be a wrong route, so it stays
Error::InvalidRequest), and a 400 about the prompt’s length is
Error::ContextLengthExceeded.
408(the server gave up waiting for the request),504(a gateway gave up waiting for the model) and524(Cloudflare’s origin timeout) are timeouts, which retry policies retry. A504or524can come after the model ran, so the request may still be billed.529is Anthropic’s overload status.499means the client closed the request (nginx;CANCELLEDingoogle.rpc.Code). It is aError::Providerwith the status, which is not retryable: the cancellation came from the caller’s side, so it is neither retried nor reported as a rejected request.501(Not Implemented) and505(HTTP Version Not Supported) areError::Providers that are not retryable: the server answers them the same way every time. A501stays a provider error rather thanError::Unsupported, whoseProviderstage comes only from an adapter’s own table of the replies it recognises (ADR 0003).
message is the adapter’s own text, such as HTTP {status}. The error
carries the status and nothing else: add the rest of its context with
error_context and Error::with_context, and mark the message
authored.
use allama::Error;
use allama_http::status_error;
assert!(matches!(status_error(504, "HTTP 504"), Error::Timeout { .. }));
assert!(matches!(status_error(529, "HTTP 529"), Error::Overloaded { .. }));
let cancelled = status_error(499, "HTTP 499");
assert!(matches!(cancelled, Error::Provider { .. }));
assert_eq!(cancelled.status(), Some(499));
assert!(!cancelled.is_retryable());
assert!(!status_error(501, "HTTP 501").is_retryable());
assert!(status_error(502, "HTTP 502").is_retryable());
assert!(matches!(status_error(307, "HTTP 307"), Error::UnexpectedResponse { .. }));