Skip to main content

status_error

Function status_error 

Source
pub fn status_error(status: u16, message: impl Into<String>) -> Error
Expand description

The error an HTTP error status indicates on its own, before the body is read.

StatusError
3xxError::UnexpectedResponse, not retryable (a redirect, which is never followed: see redirect_error)
401, 403Error::Auth
402Error::QuotaExhausted
408, 504, 524Error::Timeout
429Error::RateLimited
499Error::Provider, not retryable
other 4xxError::InvalidRequest
503, 529Error::Overloaded
501, 505Error::Provider, not retryable
anything elseError::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) and 524 (Cloudflare’s origin timeout) are timeouts, which retry policies retry. A 504 or 524 can come after the model ran, so the request may still be billed.
  • 529 is Anthropic’s overload status.
  • 499 means the client closed the request (nginx; CANCELLED in google.rpc.Code). It is a Error::Provider with 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) and 505 (HTTP Version Not Supported) are Error::Providers that are not retryable: the server answers them the same way every time. A 501 stays a provider error rather than Error::Unsupported, whose Provider stage 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 { .. }));