Skip to main content

robinhood_chain/
error.rs

1use thiserror::Error;
2
3/// Error type returned by every fallible operation in the SDK.
4#[derive(Debug, Error)]
5pub enum RobinhoodChainError {
6    /// API key was missing or malformed at construction time.
7    ///
8    /// Get a free key at <https://madeonsol.com/pricing> — Robinhood Chain
9    /// coverage is bundled into every tier at no extra cost.
10    #[error(
11        "RobinhoodChain: apiKey is required and must start with `msk_`. \
12         Get a free key at https://madeonsol.com/pricing"
13    )]
14    MissingApiKey,
15
16    /// API returned a non-2xx HTTP status.
17    #[error("Robinhood Chain API error ({status}): {message}")]
18    Api {
19        status: u16,
20        message: String,
21        body: serde_json::Value,
22    },
23
24    /// `reqwest` transport error (DNS, TLS, connection reset, etc).
25    #[error("Robinhood Chain transport error: {0}")]
26    Transport(#[from] reqwest::Error),
27
28    /// JSON serialization or deserialization error.
29    #[error("Robinhood Chain JSON error: {0}")]
30    Json(#[from] serde_json::Error),
31
32    /// URL-building error (only fires for impossible parameter combinations).
33    #[error("Robinhood Chain URL error: {0}")]
34    Url(#[from] url::ParseError),
35}
36
37impl RobinhoodChainError {
38    /// Returns the HTTP status code if this is an [`RobinhoodChainError::Api`] variant.
39    pub fn status(&self) -> Option<u16> {
40        match self {
41            RobinhoodChainError::Api { status, .. } => Some(*status),
42            _ => None,
43        }
44    }
45
46    /// Returns the raw response body if this is an [`RobinhoodChainError::Api`] variant.
47    pub fn body(&self) -> Option<&serde_json::Value> {
48        match self {
49            RobinhoodChainError::Api { body, .. } => Some(body),
50            _ => None,
51        }
52    }
53}
54
55pub type Result<T> = std::result::Result<T, RobinhoodChainError>;