Skip to main content

highflame_shield/
error.rs

1//! Error types for the Shield SDK.
2
3use thiserror::Error;
4
5/// All errors that can be returned by [`ShieldClient`](crate::ShieldClient).
6#[derive(Debug, Error)]
7pub enum ShieldError {
8    /// The API returned an RFC 9457 Problem Details response.
9    #[error("[{status}] {title}: {detail}")]
10    Api {
11        /// HTTP status code.
12        status: u16,
13        /// Short title from the Problem Details body.
14        title: String,
15        /// Human-readable explanation.
16        detail: String,
17    },
18
19    /// A network or transport error (connection refused, timeout, TLS, …).
20    #[error("connection error: {0}")]
21    Connection(String),
22
23    /// The response body could not be deserialised.
24    #[error("deserialisation error: {0}")]
25    Deserialisation(#[from] serde_json::Error),
26}
27
28impl ShieldError {
29    /// Returns the HTTP status code if this is an [`ShieldError::Api`] error.
30    pub fn status(&self) -> Option<u16> {
31        match self {
32            ShieldError::Api { status, .. } => Some(*status),
33            _ => None,
34        }
35    }
36
37    /// Returns `true` if this is an API-level error (HTTP 4xx / 5xx).
38    pub fn is_api_error(&self) -> bool {
39        matches!(self, ShieldError::Api { .. })
40    }
41
42    /// Returns `true` if this is a connection / transport error.
43    pub fn is_connection_error(&self) -> bool {
44        matches!(self, ShieldError::Connection(_))
45    }
46}
47
48impl From<reqwest::Error> for ShieldError {
49    fn from(e: reqwest::Error) -> Self {
50        ShieldError::Connection(e.to_string())
51    }
52}