Skip to main content

coman/core/
errors.rs

1/// Errors that can occur during collection operations
2#[derive(Debug)]
3pub enum CollectionError {
4    /// Collection was not found
5    CollectionNotFound(String),
6    /// Endpoint was not found
7    EndpointNotFound(String),
8    /// IO error occurred
9    IoError(std::io::Error),
10    /// JSON serialization/deserialization error
11    JsonError(serde_json::Error),
12    /// Generic error with message
13    Other(String),
14}
15
16impl std::fmt::Display for CollectionError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            CollectionError::CollectionNotFound(name) => {
20                write!(f, "Collection not found: {}", name)
21            }
22            CollectionError::EndpointNotFound(name) => write!(f, "Endpoint not found: {}", name),
23            CollectionError::IoError(e) => write!(f, "IO error: {}", e),
24            CollectionError::JsonError(e) => write!(f, "JSON error: {}", e),
25            CollectionError::Other(msg) => write!(f, "{}", msg),
26        }
27    }
28}
29
30impl std::error::Error for CollectionError {}
31
32impl From<std::io::Error> for CollectionError {
33    fn from(err: std::io::Error) -> Self {
34        CollectionError::IoError(err)
35    }
36}
37
38impl From<serde_json::Error> for CollectionError {
39    fn from(err: serde_json::Error) -> Self {
40        CollectionError::JsonError(err)
41    }
42}
43
44impl From<Box<dyn std::error::Error>> for CollectionError {
45    fn from(err: Box<dyn std::error::Error>) -> Self {
46        CollectionError::Other(err.to_string())
47    }
48}
49
50/// Errors that can occur during HTTP operations
51#[derive(Debug)]
52pub enum HttpError {
53    /// Request timed out
54    Timeout,
55    /// Connection error
56    ConnectionError(String),
57    /// Redirect error
58    RedirectError(String),
59    /// Request building error
60    RequestError(String),
61    /// Response error
62    ResponseError(String),
63    /// Generic error
64    Other(String),
65}
66
67impl std::fmt::Display for HttpError {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        match self {
70            HttpError::Timeout => write!(f, "Request timed out"),
71            HttpError::ConnectionError(msg) => write!(f, "Connection error: {}", msg),
72            HttpError::RedirectError(msg) => write!(f, "Redirect error: {}", msg),
73            HttpError::RequestError(msg) => write!(f, "Request error: {}", msg),
74            HttpError::ResponseError(msg) => write!(f, "Response error: {}", msg),
75            HttpError::Other(msg) => write!(f, "{}", msg),
76        }
77    }
78}
79
80impl std::error::Error for HttpError {}
81
82impl From<reqwest::Error> for HttpError {
83    fn from(err: reqwest::Error) -> Self {
84        if err.is_timeout() {
85            HttpError::Timeout
86        } else if err.is_connect() {
87            HttpError::ConnectionError(err.to_string())
88        } else if err.is_redirect() {
89            HttpError::RedirectError(err.to_string())
90        } else {
91            HttpError::Other(err.to_string())
92        }
93    }
94}