Skip to main content

otelite_client/
error.rs

1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, Error)]
6pub enum Error {
7    #[error("API error: {0}")]
8    ApiError(String),
9    #[error("Connection error: {0}")]
10    ConnectionError(String),
11    #[error("Not found: {0}")]
12    NotFound(String),
13    #[error("HTTP error: {0}")]
14    HttpError(#[source] reqwest::Error),
15    #[error("JSON error: {0}")]
16    ParseError(#[from] serde_json::Error),
17}
18
19impl From<reqwest::Error> for Error {
20    fn from(err: reqwest::Error) -> Self {
21        if err.is_connect() || err.is_timeout() {
22            Error::ConnectionError(format!(
23                "Failed to connect to Otelite backend. Is the server running? Error: {}",
24                err
25            ))
26        } else if err.is_status() {
27            if let Some(status) = err.status() {
28                if status.as_u16() == 404 {
29                    Error::NotFound("Resource not found".to_string())
30                } else {
31                    Error::ApiError(format!("HTTP {}: {}", status, err))
32                }
33            } else {
34                Error::ApiError(err.to_string())
35            }
36        } else {
37            Error::HttpError(err)
38        }
39    }
40}