ostium_rust_sdk/
error.rs

1//! Error types for the Ostium SDK
2//!
3//! This module defines all error types that can occur when using the SDK.
4
5use thiserror::Error;
6
7/// Type alias for Results returned by the SDK
8pub type Result<T> = std::result::Result<T, OstiumError>;
9
10/// Main error type for the Ostium SDK
11#[derive(Error, Debug)]
12pub enum OstiumError {
13    /// Network-related errors (RPC, connection issues)
14    #[error("Network error: {0}")]
15    Network(String),
16
17    /// Smart contract interaction errors
18    #[error("Contract error: {0}")]
19    Contract(String),
20
21    /// GraphQL API errors
22    #[error("GraphQL error: {0}")]
23    GraphQL(String),
24
25    /// Configuration errors
26    #[error("Configuration error: {0}")]
27    Config(String),
28
29    /// Validation errors for input parameters
30    #[error("Validation error: {0}")]
31    Validation(String),
32
33    /// Errors related to signing and wallet operations
34    #[error("Wallet error: {0}")]
35    Wallet(String),
36
37    /// HTTP request errors
38    #[error("HTTP error: {0}")]
39    Http(#[from] reqwest::Error),
40
41    /// JSON serialization/deserialization errors
42    #[error("JSON error: {0}")]
43    Json(#[from] serde_json::Error),
44
45    /// Decimal conversion errors
46    #[error("Decimal error: {0}")]
47    Decimal(#[from] rust_decimal::Error),
48
49    /// Alloy provider errors
50    #[error("Provider error: {0}")]
51    Provider(String),
52
53    /// Generic errors with context
54    #[error("{0}")]
55    Other(String),
56}
57
58impl OstiumError {
59    /// Create a new network error
60    pub fn network<S: Into<String>>(msg: S) -> Self {
61        Self::Network(msg.into())
62    }
63
64    /// Create a new contract error
65    pub fn contract<S: Into<String>>(msg: S) -> Self {
66        Self::Contract(msg.into())
67    }
68
69    /// Create a new GraphQL error
70    pub fn graphql<S: Into<String>>(msg: S) -> Self {
71        Self::GraphQL(msg.into())
72    }
73
74    /// Create a new configuration error
75    pub fn config<S: Into<String>>(msg: S) -> Self {
76        Self::Config(msg.into())
77    }
78
79    /// Create a new validation error
80    pub fn validation<S: Into<String>>(msg: S) -> Self {
81        Self::Validation(msg.into())
82    }
83
84    /// Create a new wallet error
85    pub fn wallet<S: Into<String>>(msg: S) -> Self {
86        Self::Wallet(msg.into())
87    }
88
89    /// Create a new provider error
90    pub fn provider<S: Into<String>>(msg: S) -> Self {
91        Self::Provider(msg.into())
92    }
93
94    /// Create a generic error with context
95    pub fn other<S: Into<String>>(msg: S) -> Self {
96        Self::Other(msg.into())
97    }
98
99    /// Create a new conversion error
100    pub fn conversion<S: Into<String>>(msg: S) -> Self {
101        Self::Other(format!("Conversion error: {}", msg.into()))
102    }
103
104    /// Create a new parsing error with a standardized message format.
105    ///
106    /// This constructor is used when there are issues parsing data, such as:
107    /// - Converting between data formats (e.g., Python ABIs to JSON)
108    /// - Parsing contract responses
109    /// - Handling malformed input data
110    ///
111    /// The error message will be prefixed with "Parsing error: " for consistent error handling.
112    pub fn parsing<S: Into<String>>(msg: S) -> Self {
113        Self::Other(format!("Parsing error: {}", msg.into()))
114    }
115}