cstats_core/
error.rs

1//! Error types for cstats-core
2
3use std::fmt;
4
5/// Result type alias for cstats operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Main error type for cstats operations
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// Configuration errors
12    #[error("Configuration error: {0}")]
13    Config(String),
14
15    /// HTTP client errors
16    #[error("HTTP request error: {0}")]
17    Http(#[from] reqwest::Error),
18
19    /// JSON serialization/deserialization errors
20    #[error("JSON error: {0}")]
21    Json(#[from] serde_json::Error),
22
23    /// I/O errors
24    #[error("I/O error: {0}")]
25    Io(#[from] std::io::Error),
26
27    /// Cache operation errors
28    #[error("Cache error: {0}")]
29    Cache(String),
30
31    /// Statistics calculation errors
32    #[error("Statistics error: {0}")]
33    Statistics(String),
34
35    /// API errors
36    #[error("API error: {0}")]
37    Api(String),
38
39    /// Generic errors
40    #[error("Error: {0}")]
41    Other(#[from] anyhow::Error),
42}
43
44impl Error {
45    /// Create a new configuration error
46    pub fn config<T: fmt::Display>(msg: T) -> Self {
47        Self::Config(msg.to_string())
48    }
49
50    /// Create a new cache error
51    pub fn cache<T: fmt::Display>(msg: T) -> Self {
52        Self::Cache(msg.to_string())
53    }
54
55    /// Create a new statistics error
56    pub fn statistics<T: fmt::Display>(msg: T) -> Self {
57        Self::Statistics(msg.to_string())
58    }
59
60    /// Create a new API error
61    pub fn api<T: fmt::Display>(msg: T) -> Self {
62        Self::Api(msg.to_string())
63    }
64}