Skip to main content

sal_kubernetes/
error.rs

1//! Error types for SAL Kubernetes operations
2
3use thiserror::Error;
4
5/// Errors that can occur during Kubernetes operations
6#[derive(Error, Debug)]
7pub enum KubernetesError {
8    /// Kubernetes API client error
9    #[error("Kubernetes API error: {0}")]
10    ApiError(#[from] kube::Error),
11
12    /// Configuration error
13    #[error("Configuration error: {0}")]
14    ConfigError(String),
15
16    /// Resource not found error
17    #[error("Resource not found: {0}")]
18    ResourceNotFound(String),
19
20    /// Invalid resource name or pattern
21    #[error("Invalid resource name or pattern: {0}")]
22    InvalidResourceName(String),
23
24    /// Regular expression error
25    #[error("Regular expression error: {0}")]
26    RegexError(#[from] regex::Error),
27
28    /// Serialization/deserialization error
29    #[error("Serialization error: {0}")]
30    SerializationError(#[from] serde_json::Error),
31
32    /// YAML parsing error
33    #[error("YAML error: {0}")]
34    YamlError(#[from] serde_yaml::Error),
35
36    /// Generic operation error
37    #[error("Operation failed: {0}")]
38    OperationError(String),
39
40    /// Namespace error
41    #[error("Namespace error: {0}")]
42    NamespaceError(String),
43
44    /// Permission denied error
45    #[error("Permission denied: {0}")]
46    PermissionDenied(String),
47
48    /// Timeout error
49    #[error("Operation timed out: {0}")]
50    Timeout(String),
51
52    /// Generic error wrapper
53    #[error("Generic error: {0}")]
54    Generic(#[from] anyhow::Error),
55}
56
57impl KubernetesError {
58    /// Create a new configuration error
59    pub fn config_error(msg: impl Into<String>) -> Self {
60        Self::ConfigError(msg.into())
61    }
62
63    /// Create a new operation error
64    pub fn operation_error(msg: impl Into<String>) -> Self {
65        Self::OperationError(msg.into())
66    }
67
68    /// Create a new namespace error
69    pub fn namespace_error(msg: impl Into<String>) -> Self {
70        Self::NamespaceError(msg.into())
71    }
72
73    /// Create a new permission denied error
74    pub fn permission_denied(msg: impl Into<String>) -> Self {
75        Self::PermissionDenied(msg.into())
76    }
77
78    /// Create a new timeout error
79    pub fn timeout(msg: impl Into<String>) -> Self {
80        Self::Timeout(msg.into())
81    }
82}
83
84/// Result type for Kubernetes operations
85pub type KubernetesResult<T> = Result<T, KubernetesError>;