Skip to main content

prax_cassandra/
error.rs

1//! Error types for the prax-cassandra driver.
2
3use std::time::Duration;
4
5/// Errors produced by the prax-cassandra driver.
6#[derive(Debug, thiserror::Error)]
7pub enum CassandraError {
8    /// A connection-level failure (network, TCP, cluster resolution).
9    #[error("Connection error: {0}")]
10    Connection(String),
11
12    /// Authentication was rejected by the cluster.
13    #[error("Authentication failed: {0}")]
14    Authentication(String),
15
16    /// A query failed to execute.
17    #[error("Query execution failed: {0}")]
18    Query(String),
19
20    /// A row could not be deserialized into the requested type.
21    #[error("Row deserialization failed: {0}")]
22    Deserialization(String),
23
24    /// An operation exceeded its timeout.
25    #[error("Timeout after {duration:?}: {operation}")]
26    Timeout {
27        /// Name of the operation that timed out.
28        operation: String,
29        /// Elapsed duration before timeout.
30        duration: Duration,
31    },
32
33    /// The provided configuration was invalid.
34    #[error("Configuration error: {0}")]
35    Config(String),
36
37    /// A TLS error occurred during connection setup.
38    #[error("TLS error: {0}")]
39    Tls(String),
40
41    /// A lightweight transaction did not apply (CAS failed).
42    #[error("Lightweight transaction not applied")]
43    LwtNotApplied,
44}
45
46/// Convenience alias for `Result<T, CassandraError>`.
47pub type CassandraResult<T> = Result<T, CassandraError>;
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_connection_error_display() {
55        let err = CassandraError::Connection("refused".into());
56        assert_eq!(err.to_string(), "Connection error: refused");
57    }
58
59    #[test]
60    fn test_timeout_error_display() {
61        let err = CassandraError::Timeout {
62            operation: "query".into(),
63            duration: Duration::from_secs(5),
64        };
65        assert!(err.to_string().contains("query"));
66        assert!(err.to_string().contains("5s"));
67    }
68
69    #[test]
70    fn test_lwt_not_applied_is_no_data() {
71        let err = CassandraError::LwtNotApplied;
72        assert_eq!(err.to_string(), "Lightweight transaction not applied");
73    }
74}