Skip to main content

open_library_api_rs/
error.rs

1// v0.0.1
2use thiserror::Error;
3
4/// All errors that can be returned by this library.
5#[derive(Debug, Error)]
6pub enum Error {
7    /// Input validation failed before any network call was made.
8    #[error("invalid input: {0}")]
9    InvalidInput(String),
10
11    /// A network or transport-level error from reqwest.
12    #[error("HTTP error: {0}")]
13    Http(#[from] reqwest::Error),
14
15    /// The server returned a non-2xx status code.
16    #[error("server returned {code} for {url}")]
17    Status { code: u16, url: String },
18
19    /// The server returned 404 Not Found.
20    #[error("not found: {0}")]
21    NotFound(String),
22
23    /// The server returned 429 Too Many Requests, or the local rate limiter
24    /// rejected the call.
25    #[error("rate limited")]
26    RateLimited,
27
28    /// The response body could not be parsed as the expected JSON type.
29    #[error("deserialize error: {source} — body: {body}")]
30    Deserialize {
31        source: serde_json::Error,
32        body: String,
33    },
34
35    /// The response body exceeded the 10 MB safety cap.
36    #[error("response body exceeded the 10 MB size limit")]
37    ResponseTooLarge,
38
39    /// The connection or read timeout was exceeded.
40    #[error("request timed out")]
41    Timeout,
42
43    /// URL construction failed (should not happen with valid inputs).
44    #[error("URL error: {0}")]
45    Url(#[from] url::ParseError),
46}
47
48/// Convenience alias used throughout the library.
49pub type Result<T> = std::result::Result<T, Error>;