Skip to main content

kafka_backup_core/
error.rs

1//! Error types for the Kafka backup core library.
2
3use thiserror::Error;
4
5/// Result type alias using the library's Error type.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Main error type for the Kafka backup library.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Configuration error
12    #[error("Configuration error: {0}")]
13    Config(String),
14
15    /// Kafka protocol error
16    #[error("Kafka error: {0}")]
17    Kafka(#[from] KafkaError),
18
19    /// Storage error
20    #[error("Storage error: {0}")]
21    Storage(#[from] StorageError),
22
23    /// Compression error
24    #[error("Compression error: {0}")]
25    Compression(String),
26
27    /// Serialization error
28    #[error("Serialization error: {0}")]
29    Serialization(String),
30
31    /// IO error
32    #[error("IO error: {0}")]
33    Io(#[from] std::io::Error),
34
35    /// Manifest error
36    #[error("Manifest error: {0}")]
37    Manifest(String),
38
39    /// Topic not found
40    #[error("Topic not found: {0}")]
41    TopicNotFound(String),
42
43    /// Backup not found
44    #[error("Backup not found: {0}")]
45    BackupNotFound(String),
46
47    /// Connection error
48    #[error("Connection error: {0}")]
49    Connection(String),
50
51    /// Authentication error
52    #[error("Authentication error: {0}")]
53    Authentication(String),
54}
55
56/// Kafka-specific errors
57#[derive(Error, Debug)]
58#[non_exhaustive]
59pub enum KafkaError {
60    /// Connection failed
61    #[error("Failed to connect to broker {broker}: {message}")]
62    ConnectionFailed { broker: String, message: String },
63
64    /// Protocol error
65    #[error("Protocol error: {0}")]
66    Protocol(String),
67
68    /// Broker error response
69    #[error("Broker returned error code {code}: {message}")]
70    BrokerError { code: i16, message: String },
71
72    /// Timeout
73    #[error("Operation timed out: {0}")]
74    Timeout(String),
75
76    /// No available brokers
77    #[error("No available brokers")]
78    NoBrokersAvailable,
79
80    /// Topic does not exist
81    #[error("Topic does not exist: {0}")]
82    TopicNotExists(String),
83
84    /// Partition not available
85    #[error("Partition {partition} not available for topic {topic}")]
86    PartitionNotAvailable { topic: String, partition: i32 },
87
88    /// TLS configuration error
89    #[error("TLS configuration error: {0}")]
90    TlsConfig(String),
91
92    /// Certificate loading error
93    #[error("Failed to load certificate from {path}: {message}")]
94    CertificateLoad { path: String, message: String },
95
96    /// Private key loading error
97    #[error("Failed to load private key from {path}: {message}")]
98    PrivateKeyLoad { path: String, message: String },
99}
100
101/// Storage-specific errors
102#[derive(Error, Debug)]
103pub enum StorageError {
104    /// Object not found
105    #[error("Object not found: {0}")]
106    NotFound(String),
107
108    /// Permission denied
109    #[error("Permission denied: {0}")]
110    PermissionDenied(String),
111
112    /// Storage backend error
113    #[error("Backend error: {0}")]
114    Backend(String),
115
116    /// Invalid path
117    #[error("Invalid path: {0}")]
118    InvalidPath(String),
119}
120
121impl From<serde_json::Error> for Error {
122    fn from(err: serde_json::Error) -> Self {
123        Error::Serialization(err.to_string())
124    }
125}
126
127impl From<serde_yaml::Error> for Error {
128    fn from(err: serde_yaml::Error) -> Self {
129        Error::Serialization(err.to_string())
130    }
131}
132
133impl From<sqlx::Error> for Error {
134    fn from(err: sqlx::Error) -> Self {
135        Error::Storage(StorageError::Backend(err.to_string()))
136    }
137}