Skip to main content

enya_client/
error.rs

1//! Error types for the metrics client.
2
3use std::fmt;
4
5/// Errors that can occur when executing metrics queries.
6#[derive(Debug, Clone)]
7pub enum ClientError {
8    /// The query could not be translated to the backend's query language.
9    TranslationError(String),
10    /// Network error during request.
11    NetworkError(String),
12    /// Backend returned an error response.
13    BackendError { status: u16, message: String },
14    /// Failed to parse the backend's response.
15    ParseError(String),
16    /// Query timed out waiting for a response.
17    Timeout {
18        /// How long we waited before timing out.
19        elapsed_secs: u64,
20        /// The configured timeout threshold.
21        timeout_secs: u64,
22    },
23    /// Invalid request parameters.
24    InvalidRequest(String),
25}
26
27impl fmt::Display for ClientError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::TranslationError(msg) => write!(f, "query translation failed: {msg}"),
31            Self::NetworkError(msg) => write!(f, "network error: {msg}"),
32            Self::BackendError { status, message } => {
33                write!(f, "backend error (HTTP {status}): {message}")
34            }
35            Self::ParseError(msg) => write!(f, "failed to parse response: {msg}"),
36            Self::Timeout {
37                elapsed_secs,
38                timeout_secs,
39            } => write!(
40                f,
41                "query timed out after {elapsed_secs}s (limit: {timeout_secs}s)"
42            ),
43            Self::InvalidRequest(msg) => write!(f, "invalid request: {msg}"),
44        }
45    }
46}
47
48impl std::error::Error for ClientError {}