Skip to main content

Module error

Module error 

Source
Expand description

What a call can fail with, and how a failure renders.

Error is the one error type every fallible operation returns. It is a single boxed pointer, so Result<T, Error> costs a pointer beside T rather than the size of the largest failure; the detail lives behind Error::kind.

Four things here are not what a plain thiserror enum would do:

  • An API failure’s message is extracted from the server’s body in a fixed order. Servers put the human-readable sentence under error, error.message, message, detail, detail.message or a list under detail, and the SDK reads them in that order so the message a caller sees does not depend on which shape the endpoint chose. See ApiError::message.
  • Nothing here prints a header value or a codec error’s input. An Authorization header and a decode error’s excerpt of the body are both in reach of these types, and neither appears in Debug or Display.
  • Text the server chose is escaped and cut before it is printed. An API failure’s message and the request id are the server’s text, and either would otherwise put a line break, a terminal colour or 16 MiB into every log line that prints the error. The accessors for the body and the headers still return them as they arrived.
  • Retry-After is parsed against a caller-supplied now. Reading the clock inside the parser would make the HTTP-date case untestable without mocking time, so the parser takes the instant to measure against and ApiError::retry_after is the thin wrapper that reads the clock.

Structs§

ApiError
An unsuccessful HTTP response, with the body and metadata it came with.
Error
Anything a call to the API can fail with.
ResponseValidationError
A successful HTTP response whose body this SDK could not read.

Enums§

ApiErrorKind
Which class of API failure a status code puts a response in.
ErrorKind
Which kind of failure an Error is.