coman/core/
errors.rs

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