Skip to main content

origin_asset/
error.rs

1use thiserror::Error;
2
3/// Unified error type for all Origin SDK operations.
4#[derive(Debug, Error)]
5pub enum OriginError {
6    /// HTTP transport error (connection, timeout, TLS, etc.).
7    #[error("HTTP error: {0}")]
8    Http(#[from] reqwest::Error),
9
10    /// The API returned a non-success response.
11    #[error("API error ({status}): {message}")]
12    Api {
13        status: u16,
14        code: Option<String>,
15        message: String,
16        command: Option<String>,
17    },
18
19    /// JSON serialization / deserialization error.
20    #[error("JSON error: {0}")]
21    Json(#[from] serde_json::Error),
22
23    /// File I/O error (e.g. reading a file for upload).
24    #[error("IO error: {0}")]
25    Io(#[from] std::io::Error),
26
27    /// Configuration error (missing key, invalid URL, etc.).
28    #[error("{0}")]
29    Config(String),
30}
31
32impl OriginError {
33    pub fn api(status: u16, message: impl Into<String>) -> Self {
34        Self::Api {
35            status,
36            code: None,
37            message: message.into(),
38            command: None,
39        }
40    }
41
42    pub fn api_full(
43        status: u16,
44        code: Option<String>,
45        message: impl Into<String>,
46        command: Option<String>,
47    ) -> Self {
48        Self::Api {
49            status,
50            code,
51            message: message.into(),
52            command,
53        }
54    }
55
56    pub fn config(message: impl Into<String>) -> Self {
57        Self::Config(message.into())
58    }
59}
60
61/// Convenience alias used throughout the SDK.
62pub type Result<T> = std::result::Result<T, OriginError>;