Skip to main content

volt_client_grpc/
error.rs

1//! Error types for the Volt client library.
2
3use thiserror::Error;
4
5/// Result type alias using VoltError
6pub type Result<T> = std::result::Result<T, VoltError>;
7
8/// Errors that can occur when using the Volt client
9#[derive(Error, Debug)]
10pub enum VoltError {
11    /// Configuration error
12    #[error("Configuration error: {0}")]
13    ConfigError(String),
14
15    /// Missing required configuration field
16    #[error("Missing required configuration: {0}")]
17    MissingConfig(String),
18
19    /// Authentication failed
20    #[error("Authentication failed: {0}")]
21    AuthenticationFailed(String),
22
23    /// Connection error
24    #[error("Connection error: {0}")]
25    ConnectionError(String),
26
27    /// gRPC error
28    #[error("gRPC error: {0}")]
29    GrpcError(#[from] tonic::Status),
30
31    /// Transport error
32    #[error("Transport error: {0}")]
33    TransportError(#[from] tonic::transport::Error),
34
35    /// IO error
36    #[error("IO error: {0}")]
37    IoError(#[from] std::io::Error),
38
39    /// JSON serialization/deserialization error
40    #[error("JSON error: {0}")]
41    JsonError(#[from] serde_json::Error),
42
43    /// Cryptographic operation error
44    #[error("Crypto error: {0}")]
45    CryptoError(String),
46
47    /// Key error
48    #[error("Key error: {0}")]
49    KeyError(String),
50
51    /// Certificate error
52    #[error("Certificate error: {0}")]
53    CertificateError(String),
54
55    /// JWT error
56    #[error("JWT error: {0}")]
57    JwtError(#[from] jsonwebtoken::errors::Error),
58
59    /// Session error
60    #[error("Session error: {0}")]
61    SessionError(String),
62
63    /// Method not found
64    #[error("Method not found: {0}")]
65    MethodNotFound(String),
66
67    /// Invalid response
68    #[error("Invalid response: {0}")]
69    InvalidResponse(String),
70
71    /// Timeout error
72    #[error("Timeout: {0}")]
73    Timeout(String),
74
75    /// Request denied
76    #[error("Request denied: {0}")]
77    Denied(String),
78
79    /// Protocol error
80    #[error("Protocol error: {0}")]
81    ProtocolError(String),
82
83    /// DID resolution error
84    #[error("DID resolution error: {0}")]
85    DidResolutionError(String),
86
87    /// HTTP error (for DID resolution, etc.)
88    #[error("HTTP error: {0}")]
89    HttpError(#[from] reqwest::Error),
90
91    /// Server returned an error
92    #[error("Server error: {0}")]
93    ServerError(String),
94
95    /// Permission denied
96    #[error("Permission denied: {0}")]
97    PermissionDenied(String),
98
99    /// Y.js/yrs error
100    #[error("CRDT error: {0}")]
101    CrdtError(String),
102
103    /// Invalid argument
104    #[error("Invalid argument: {0}")]
105    InvalidArgument(String),
106
107    /// Not initialized
108    #[error("Client not initialized")]
109    NotInitialized,
110
111    /// Not connected
112    #[error("Client not connected")]
113    NotConnected,
114
115    /// Already connected
116    #[error("Client already connected")]
117    AlreadyConnected,
118
119    /// Signature verification failed
120    #[error("Signature verification failed")]
121    SignatureVerificationFailed,
122
123    /// Encryption error
124    #[error("Encryption error: {0}")]
125    EncryptionError(String),
126
127    /// Decryption error
128    #[error("Decryption error: {0}")]
129    DecryptionError(String),
130
131    /// Base64 decode error
132    #[error("Base64 decode error: {0}")]
133    Base64Error(#[from] base64::DecodeError),
134
135    /// Internal error
136    #[error("Internal error: {0}")]
137    Internal(String),
138}
139
140impl VoltError {
141    /// Create a configuration error
142    pub fn config<S: Into<String>>(msg: S) -> Self {
143        VoltError::ConfigError(msg.into())
144    }
145
146    /// Create a missing configuration error
147    pub fn missing_config<S: Into<String>>(field: S) -> Self {
148        VoltError::MissingConfig(field.into())
149    }
150
151    /// Create an authentication error
152    pub fn auth<S: Into<String>>(msg: S) -> Self {
153        VoltError::AuthenticationFailed(msg.into())
154    }
155
156    /// Create a connection error
157    pub fn connection<S: Into<String>>(msg: S) -> Self {
158        VoltError::ConnectionError(msg.into())
159    }
160
161    /// Create a crypto error
162    pub fn crypto<S: Into<String>>(msg: S) -> Self {
163        VoltError::CryptoError(msg.into())
164    }
165
166    /// Create a key error
167    pub fn key<S: Into<String>>(msg: S) -> Self {
168        VoltError::KeyError(msg.into())
169    }
170
171    /// Create a session error
172    pub fn session<S: Into<String>>(msg: S) -> Self {
173        VoltError::SessionError(msg.into())
174    }
175
176    /// Create an internal error
177    pub fn internal<S: Into<String>>(msg: S) -> Self {
178        VoltError::Internal(msg.into())
179    }
180
181    /// Create a serialization error
182    pub fn serialization<S: Into<String>>(msg: S) -> Self {
183        VoltError::InvalidResponse(msg.into())
184    }
185
186    /// Create a protocol error
187    pub fn protocol<S: Into<String>>(msg: S) -> Self {
188        VoltError::ProtocolError(msg.into())
189    }
190
191    /// Create a server error with code
192    pub fn server(code: i32, msg: &str) -> Self {
193        VoltError::ServerError(format!("Code {}: {}", code, msg))
194    }
195
196    /// Create a gRPC error
197    pub fn grpc(code: tonic::Code, msg: &str) -> Self {
198        VoltError::GrpcError(tonic::Status::new(code, msg.to_string()))
199    }
200}