distributed_config/
error.rs

1//! Error types for the distributed-config library
2
3use thiserror::Error;
4
5/// Result type alias for this crate
6pub type Result<T> = std::result::Result<T, ConfigError>;
7
8/// Main error type for configuration operations
9#[derive(Debug, Error)]
10pub enum ConfigError {
11    /// Error reading or parsing configuration file
12    #[error("Configuration file error: {0}")]
13    FileError(#[from] std::io::Error),
14
15    /// Error serializing or deserializing configuration
16    #[error("Serialization error: {0}")]
17    SerializationError(#[from] serde_json::Error),
18
19    /// Error parsing YAML configuration
20    #[error("YAML parsing error: {0}")]
21    YamlError(#[from] serde_yaml::Error),
22
23    /// Error parsing TOML configuration
24    #[error("TOML parsing error: {0}")]
25    TomlError(#[from] toml::de::Error),
26
27    /// Configuration key not found
28    #[error("Configuration key not found: {key}")]
29    KeyNotFound { key: String },
30
31    /// Type conversion error
32    #[error("Type conversion error: cannot convert {from} to {to}")]
33    TypeConversion { from: String, to: String },
34
35    /// Schema validation error
36    #[error("Schema validation error: {0}")]
37    ValidationError(String),
38
39    /// Network error when accessing remote configuration
40    #[error("Network error: {0}")]
41    NetworkError(#[from] reqwest::Error),
42
43    /// Authentication error for remote sources
44    #[error("Authentication error: {0}")]
45    AuthenticationError(String),
46
47    /// Configuration source initialization error
48    #[error("Source initialization error: {0}")]
49    SourceInitializationError(String),
50
51    /// Configuration watcher error
52    #[error("Watcher error: {0}")]
53    WatcherError(String),
54
55    /// File system watching error
56    #[error("File system error: {0}")]
57    FileSystemError(#[from] notify::Error),
58
59    /// Distributed backend error
60    #[cfg(feature = "redis-backend")]
61    #[error("Redis error: {0}")]
62    RedisError(#[from] redis::RedisError),
63
64    /// Distributed backend error
65    #[cfg(feature = "etcd-backend")]
66    #[error("Etcd error: {0}")]
67    EtcdError(#[from] etcd_client::Error),
68
69    /// Lock acquisition timeout
70    #[error("Lock timeout: failed to acquire lock within {timeout_ms}ms")]
71    LockTimeout { timeout_ms: u64 },
72
73    /// Configuration conflict in distributed environment
74    #[error("Configuration conflict: {0}")]
75    ConflictError(String),
76
77    /// Generic configuration error
78    #[error("Configuration error: {0}")]
79    Other(String),
80}
81
82impl ConfigError {
83    /// Create a new validation error
84    pub fn validation_error<S: Into<String>>(msg: S) -> Self {
85        ConfigError::ValidationError(msg.into())
86    }
87
88    /// Create a new authentication error
89    pub fn auth_error<S: Into<String>>(msg: S) -> Self {
90        ConfigError::AuthenticationError(msg.into())
91    }
92
93    /// Create a new source initialization error
94    pub fn source_init_error<S: Into<String>>(msg: S) -> Self {
95        ConfigError::SourceInitializationError(msg.into())
96    }
97
98    /// Create a new watcher error
99    pub fn watcher_error<S: Into<String>>(msg: S) -> Self {
100        ConfigError::WatcherError(msg.into())
101    }
102
103    /// Create a new conflict error
104    pub fn conflict_error<S: Into<String>>(msg: S) -> Self {
105        ConfigError::ConflictError(msg.into())
106    }
107
108    /// Create a new generic error
109    pub fn other<S: Into<String>>(msg: S) -> Self {
110        ConfigError::Other(msg.into())
111    }
112}
113
114impl From<anyhow::Error> for ConfigError {
115    fn from(err: anyhow::Error) -> Self {
116        ConfigError::Other(err.to_string())
117    }
118}
119
120impl From<jsonschema::ValidationError<'_>> for ConfigError {
121    fn from(err: jsonschema::ValidationError<'_>) -> Self {
122        ConfigError::ValidationError(err.to_string())
123    }
124}