nucleus_sdk/
error.rs

1use thiserror::Error;
2
3/// Base error type for all SDK errors
4#[derive(Error, Debug)]
5pub enum SdkError {
6    #[error("{0}")]
7    User(#[from] UserError),
8    
9    #[error("{0}")]
10    Api(#[from] APIError),
11
12    #[error("{0}")]
13    Transaction(String),
14    
15    #[error("{0}")]
16    Protocol(#[from] ProtocolError),
17}
18
19/// Error type for user-related errors
20#[derive(Error, Debug)]
21pub enum UserError {
22    #[error("{0}")]
23    InvalidInputs(String),
24    
25    #[error("{0}")]
26    Other(String),
27}
28
29/// Error type for API-returned errors
30#[derive(Error, Debug)]
31pub struct APIError {
32    message: String,
33    status_code: i32,
34}
35
36impl APIError {
37    pub fn new(message: String, status_code: i32) -> Self {
38        Self {
39            message,
40            status_code,
41        }
42    }
43}
44
45impl std::fmt::Display for APIError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(f, "API Error ({}): {}", self.status_code, self.message)
48    }
49}
50
51/// Error type for protocol-related errors
52#[derive(Error, Debug)]
53pub struct ProtocolError {
54    message: String,
55}
56
57impl ProtocolError {
58    pub fn new(message: String) -> Self {
59        Self { message }
60    }
61}
62
63impl std::fmt::Display for ProtocolError {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        write!(f, "Protocol Error: {}", self.message)
66    }
67}
68
69/// Represents errors that can occur during transaction operations
70#[derive(Debug)]
71pub struct Transaction(pub String);
72
73impl std::fmt::Display for Transaction {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        write!(f, "Transaction error: {}", self.0)
76    }
77}
78
79impl std::error::Error for Transaction {}
80
81impl From<Transaction> for SdkError {
82    fn from(err: Transaction) -> Self {
83        SdkError::Transaction(err.0)
84    }
85}
86
87pub type Result<T> = std::result::Result<T, SdkError>;
88pub use UserError::InvalidInputs as InvalidInputsError;