casper_client/
error.rs

1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, CasperError>;
4
5#[derive(Error, Debug)]
6pub enum CasperError {
7    #[error("HTTP error: {0}")]
8    Http(#[from] reqwest::Error),
9    
10    #[error("JSON serialization error: {0}")]
11    Json(#[from] serde_json::Error),
12    
13    #[error("URL parsing error: {0}")]
14    Url(#[from] url::ParseError),
15    
16    #[error("Server error: {status} - {message}")]
17    Server { status: u16, message: String },
18    
19    #[error("Client error: {status} - {message}")]
20    Client { status: u16, message: String },
21    
22    #[error("Invalid response: {0}")]
23    InvalidResponse(String),
24    
25    #[error("Collection not found: {0}")]
26    CollectionNotFound(String),
27    
28    #[error("Index creation in progress")]
29    IndexCreationInProgress,
30    
31    #[error("Operation not allowed: {0}")]
32    OperationNotAllowed(String),
33    
34    #[error("Invalid vector dimension: expected {expected}, got {actual}")]
35    InvalidDimension { expected: usize, actual: usize },
36    
37    #[error("Vector ID exceeds collection max size: {id}")]
38    IdExceedsMaxSize { id: u32 },
39    
40    #[error("Zero-norm vectors are not allowed")]
41    ZeroNormVector,
42    
43    #[error("Collection is not mutable")]
44    CollectionNotMutable,
45    
46    #[error("Index already exists")]
47    IndexAlreadyExists,
48    
49    #[error("gRPC error: {0}")]
50    Grpc(String),
51    
52    #[error("Unknown error: {0}")]
53    Unknown(String),
54}
55
56impl CasperError {
57    pub fn from_status(status: u16, message: String) -> Self {
58        match status {
59            400 => CasperError::Client { status, message },
60            404 => CasperError::CollectionNotFound(message),
61            405 => CasperError::OperationNotAllowed(message),
62            409 => CasperError::IndexAlreadyExists,
63            500..=599 => CasperError::Server { status, message },
64            _ => CasperError::Unknown(format!("HTTP {}: {}", status, message)),
65        }
66    }
67}