Skip to main content

accumulate_client/
errors.rs

1//! Error types for the Accumulate Rust SDK
2
3#![allow(missing_docs)]
4
5use thiserror::Error;
6
7/// Main error type for the Accumulate SDK
8#[derive(Error, Debug)]
9pub enum Error {
10    #[error("Signature error: {0}")]
11    Signature(#[from] SignatureError),
12
13    #[error("Encoding error: {0}")]
14    Encoding(String),
15
16    #[error("Network error: {0}")]
17    Network(String),
18
19    #[error("JSON error: {0}")]
20    Json(#[from] serde_json::Error),
21
22    #[error("IO error: {0}")]
23    Io(#[from] std::io::Error),
24
25    #[error("RPC error: code={code}, message={message}")]
26    Rpc { code: i32, message: String },
27
28    #[error("Validation error: {0}")]
29    Validation(#[from] ValidationError),
30
31    #[error("General error: {0}")]
32    General(String),
33}
34
35/// Validation-specific errors for transaction bodies and headers
36#[derive(Error, Debug, Clone)]
37pub enum ValidationError {
38    #[error("Invalid URL: {0}")]
39    InvalidUrl(String),
40
41    #[error("Required field missing: {0}")]
42    RequiredFieldMissing(String),
43
44    #[error("Invalid field value: {field} - {reason}")]
45    InvalidFieldValue { field: String, reason: String },
46
47    #[error("Amount must be positive: {0}")]
48    InvalidAmount(String),
49
50    #[error("Empty collection not allowed: {0}")]
51    EmptyCollection(String),
52
53    #[error("Invalid hash: expected {expected} bytes, got {actual}")]
54    InvalidHash { expected: usize, actual: usize },
55
56    #[error("Value out of range: {field} must be between {min} and {max}")]
57    OutOfRange { field: String, min: String, max: String },
58
59    #[error("Invalid token symbol: {0}")]
60    InvalidTokenSymbol(String),
61
62    #[error("Invalid precision: must be between 0 and 18, got {0}")]
63    InvalidPrecision(u64),
64}
65
66/// Signature-specific errors
67#[derive(Error, Debug, Clone)]
68pub enum SignatureError {
69    #[error("Invalid signature format")]
70    InvalidFormat,
71
72    #[error("Verification failed: {0}")]
73    VerificationFailed(String),
74
75    #[error("Unsupported signature type: {0}")]
76    UnsupportedType(String),
77
78    #[error("Invalid public key")]
79    InvalidPublicKey,
80
81    #[error("Invalid signature bytes")]
82    InvalidSignature,
83
84    #[error("Cryptographic error: {0}")]
85    Crypto(String),
86}
87
88impl From<String> for Error {
89    fn from(s: String) -> Self {
90        Error::General(s)
91    }
92}
93
94impl From<&str> for Error {
95    fn from(s: &str) -> Self {
96        Error::General(s.to_string())
97    }
98}
99
100impl Error {
101    /// Create an RPC error with code and message
102    pub fn rpc(code: i32, message: String) -> Self {
103        Error::Rpc { code, message }
104    }
105}