Something went wrong. Try again.
A terminal UI for Libby libbui.vinnymac.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142//! The client's error type.//!//! Errors are split by what the caller can *do* about them, because the UI//! reacts differently to each: re-pair, stop everything, show the library's//! own explanation, or offer a retry.
use crate::ids::{CardId, FormatId};
/// A `Result` whose error is [`enum@Error`].pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Which service a request was addressed to.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pub enum Service { /// The account API. Sentry, /// The public catalog. Thunder, /// The library finder. Locate, /// A content or image CDN. Content,}
/// Everything that can go wrong talking to Libby.#[derive(Debug, thiserror::Error)]#[non_exhaustive]pub enum Error { /// The identity token is dead or was never linked. The only fix is to pair /// this device again. #[error("this device is no longer linked to a Libby account")] Unlinked,
/// The library suspended this card. **Fatal for the card**: no further /// requests may be made on its behalf. #[error("library card {card} has been suspended by the library")] CardSuspended { /// The suspended card. card: CardId, },
/// The library refused an action and (usually) said why. `explanation` is /// written by the library for patrons and is safe to show as-is after /// sanitizing control characters. #[error("{explanation}")] Refused { /// Machine code, e.g. `"PatronHasExceededCheckoutLimit"`. code: String, /// Human explanation; falls back to `code` when the server sent none. explanation: String, },
/// The thing asked for does not exist (title, loan, hold, library). #[error("not found: {what}")] NotFound { /// What was looked for, or the server's explanation. what: String, },
/// The server is throttling us. Honour `retry_after` if present. #[error("{service:?} is rate limiting requests")] RateLimited { /// The throttling service. service: Service, /// How long the server asked us to wait, when it said. retry_after: Option<std::time::Duration>, },
/// A status code the client has no better name for. #[error("{service:?} answered HTTP {status}: {message}")] Status { /// The service that answered. service: Service, /// The HTTP status code. status: u16, /// The server's message (sanitized), or the status reason. message: String, },
/// The response arrived but was not the shape we expect. This is a bug in /// the client or a server-side change; it is never papered over. #[error("unexpected response from {service:?} ({context}): {source}")] Decode { /// The service that answered. service: Service, /// Which response was being decoded. context: &'static str, /// The decoder's complaint. #[source] source: serde_json::Error, },
/// A response that decoded but violated an invariant (missing redirect /// target, unparseable timestamp, …). #[error("malformed response from {service:?}: {message}")] Malformed { /// The service that answered. service: Service, /// What was wrong. message: String, },
/// This format has no implemented direct fulfilment path. #[error("no implemented direct fulfilment path for {format}")] NotDownloadable { /// The format that was asked for. format: FormatId, },
/// Caller error caught before any request was made. #[error("invalid input: {0}")] InvalidInput(String),
/// The request never completed (DNS, TLS, timeout, connection reset). #[error("network error talking to {service:?}: {source}")] Network { /// The service the request was for. service: Service, /// The transport error. #[source] source: reqwest::Error, },}
impl Error { /// Whether simply trying again later could succeed. #[must_use] pub fn is_retryable(&self) -> bool { match self { Self::RateLimited { .. } | Self::Network { .. } => true, Self::Status { status, .. } => matches!(status, 500 | 502 | 503 | 504), _ => false, } }
/// Whether the user must pair this device again before anything works. #[must_use] pub fn needs_pairing(&self) -> bool { matches!(self, Self::Unlinked) }}