ig_client/
error.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 12/5/25
5******************************************************************************/
6use reqwest::StatusCode;
7use std::fmt::{Display, Formatter};
8use std::{fmt, io};
9
10/// Error type for fetch operations
11#[derive(Debug)]
12pub enum FetchError {
13    /// Network error from reqwest
14    Reqwest(reqwest::Error),
15    /// Database error from sqlx
16    Sqlx(sqlx::Error),
17    /// Error during parsing
18    Parser(String),
19}
20
21impl Display for FetchError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            FetchError::Reqwest(e) => write!(f, "network error: {e}"),
25            FetchError::Sqlx(e) => write!(f, "db error: {e}"),
26            FetchError::Parser(msg) => write!(f, "parser error: {msg}"),
27        }
28    }
29}
30
31impl std::error::Error for FetchError {}
32
33impl From<reqwest::Error> for FetchError {
34    fn from(err: reqwest::Error) -> Self {
35        FetchError::Reqwest(err)
36    }
37}
38
39impl From<sqlx::Error> for FetchError {
40    fn from(err: sqlx::Error) -> Self {
41        FetchError::Sqlx(err)
42    }
43}
44
45/// Error type for authentication operations
46#[derive(Debug)]
47pub enum AuthError {
48    /// Network error from reqwest
49    Network(reqwest::Error),
50    /// I/O error
51    Io(io::Error),
52    /// JSON serialization or deserialization error
53    Json(serde_json::Error),
54    /// Other unspecified error
55    Other(String),
56    /// Invalid credentials error
57    BadCredentials,
58    /// Unexpected HTTP status code
59    Unexpected(StatusCode),
60    /// Rate limit exceeded error
61    RateLimitExceeded,
62}
63
64impl Display for AuthError {
65    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
66        match self {
67            AuthError::Network(e) => write!(f, "network error: {e}"),
68            AuthError::Io(e) => write!(f, "io error: {e}"),
69            AuthError::Json(e) => write!(f, "json error: {e}"),
70            AuthError::Other(msg) => write!(f, "other error: {msg}"),
71            AuthError::BadCredentials => write!(f, "bad credentials"),
72            AuthError::Unexpected(s) => write!(f, "unexpected http status: {s}"),
73            AuthError::RateLimitExceeded => write!(f, "rate limit exceeded"),
74        }
75    }
76}
77
78impl std::error::Error for AuthError {}
79
80impl From<reqwest::Error> for AuthError {
81    fn from(e: reqwest::Error) -> Self {
82        AuthError::Network(e)
83    }
84}
85impl From<Box<dyn std::error::Error + Send + Sync>> for AuthError {
86    fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
87        match e.downcast::<reqwest::Error>() {
88            Ok(req) => AuthError::Network(*req),
89            Err(e) => match e.downcast::<serde_json::Error>() {
90                Ok(js) => AuthError::Json(*js),
91                Err(e) => match e.downcast::<std::io::Error>() {
92                    Ok(ioe) => AuthError::Io(*ioe),
93                    Err(other) => AuthError::Other(other.to_string()),
94                },
95            },
96        }
97    }
98}
99impl From<AppError> for AuthError {
100    fn from(e: AppError) -> Self {
101        match e {
102            AppError::Network(e) => AuthError::Network(e),
103            AppError::Io(e) => AuthError::Io(e),
104            AppError::Json(e) => AuthError::Json(e),
105            AppError::Unexpected(s) => AuthError::Unexpected(s),
106            _ => AuthError::Other("unknown error".to_string()),
107        }
108    }
109}
110
111/// General application error type
112#[derive(Debug)]
113pub enum AppError {
114    /// Network error from reqwest
115    Network(reqwest::Error),
116    /// I/O error
117    Io(io::Error),
118    /// JSON serialization or deserialization error
119    Json(serde_json::Error),
120    /// Unexpected HTTP status code
121    Unexpected(StatusCode),
122    /// Database error from sqlx
123    Db(sqlx::Error),
124    /// Unauthorized access error
125    Unauthorized,
126    /// Resource not found error
127    NotFound,
128    /// API rate limit exceeded
129    RateLimitExceeded,
130    /// Error during serialization or deserialization
131    SerializationError(String),
132    /// WebSocket communication error
133    WebSocketError(String),
134    /// Deserialization error with details
135    Deserialization(String),
136    /// Represents an error type for invalid input.
137    ///
138    /// This enum variant is used to indicate that invalid input has been provided,
139    /// typically taking the form of a string that describes the nature of the issue.
140    ///
141    /// # Variants
142    /// * `InvalidInput(String)`
143    ///   - Contains a `String` value that provides more details about why the input
144    ///     was considered invalid, such as specific formatting issues or constraints
145    ///     that were violated.
146    ///
147    InvalidInput(String),
148}
149
150impl Display for AppError {
151    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
152        match self {
153            AppError::Network(e) => write!(f, "network error: {e}"),
154            AppError::Io(e) => write!(f, "io error: {e}"),
155            AppError::Json(e) => write!(f, "json error: {e}"),
156            AppError::Unexpected(s) => write!(f, "unexpected http status: {s}"),
157            AppError::Db(e) => write!(f, "db error: {e}"),
158            AppError::Unauthorized => write!(f, "unauthorized"),
159            AppError::NotFound => write!(f, "not found"),
160            AppError::RateLimitExceeded => write!(f, "rate limit exceeded"),
161            AppError::SerializationError(s) => write!(f, "serialization error: {s}"),
162            AppError::WebSocketError(s) => write!(f, "websocket error: {s}"),
163            AppError::Deserialization(s) => write!(f, "deserialization error: {s}"),
164            AppError::InvalidInput(s) => write!(f, "invalid input: {s}"),
165        }
166    }
167}
168
169impl std::error::Error for AppError {}
170
171impl From<reqwest::Error> for AppError {
172    fn from(e: reqwest::Error) -> Self {
173        AppError::Network(e)
174    }
175}
176impl From<io::Error> for AppError {
177    fn from(e: io::Error) -> Self {
178        AppError::Io(e)
179    }
180}
181impl From<serde_json::Error> for AppError {
182    fn from(e: serde_json::Error) -> Self {
183        AppError::Json(e)
184    }
185}
186impl From<sqlx::Error> for AppError {
187    fn from(e: sqlx::Error) -> Self {
188        AppError::Db(e)
189    }
190}
191impl From<AuthError> for AppError {
192    fn from(e: AuthError) -> Self {
193        match e {
194            AuthError::Network(e) => AppError::Network(e),
195            AuthError::Io(e) => AppError::Io(e),
196            AuthError::Json(e) => AppError::Json(e),
197            AuthError::BadCredentials => AppError::Unauthorized,
198            AuthError::Unexpected(s) => AppError::Unexpected(s),
199            _ => AppError::Unexpected(StatusCode::INTERNAL_SERVER_ERROR),
200        }
201    }
202}
203
204impl From<Box<dyn std::error::Error>> for AppError {
205    fn from(e: Box<dyn std::error::Error>) -> Self {
206        match e.downcast::<reqwest::Error>() {
207            Ok(req) => AppError::Network(*req),
208            Err(e) => match e.downcast::<serde_json::Error>() {
209                Ok(js) => AppError::Json(*js),
210                Err(e) => match e.downcast::<std::io::Error>() {
211                    Ok(ioe) => AppError::Io(*ioe),
212                    Err(_) => AppError::Unexpected(StatusCode::INTERNAL_SERVER_ERROR),
213                },
214            },
215        }
216    }
217}