Skip to main content

hypixel/
error.rs

1use std::time::Duration;
2
3/// Errors returned by the Hypixel SDK.
4#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum Error {
7    /// A method requiring authentication was called without an API key configured.
8    #[error("an API key is required for this endpoint but none was configured")]
9    MissingApiKey,
10
11    /// The configured API key was rejected by the API (HTTP 403).
12    #[error("the API key was rejected (403 Forbidden)")]
13    InvalidApiKey,
14
15    /// The request was rate limited (HTTP 429).
16    #[error("rate limited; retry after {}s", retry_after.map(|d| d.as_secs()).unwrap_or(0))]
17    RateLimited {
18        /// Suggested wait duration parsed from the `Retry-After` header, if present.
19        retry_after: Option<Duration>,
20    },
21
22    /// The API responded with a non-success status and an error cause.
23    #[error("API error ({status}): {cause}")]
24    Api {
25        /// HTTP status code returned by the API.
26        status: u16,
27        /// The `cause` field from the API response body, if available.
28        cause: String,
29    },
30
31    /// The request could not be sent or the connection failed.
32    #[error("HTTP transport error: {0}")]
33    Http(#[from] reqwest::Error),
34
35    /// The response body could not be deserialized into the expected type.
36    #[error("failed to decode response: {0}")]
37    Decode(#[from] serde_json::Error),
38}
39
40/// Convenience alias for results returned by this crate.
41pub type Result<T> = std::result::Result<T, Error>;