polars_redis/
error.rs

1//! Error types for polars-redis.
2
3use thiserror::Error;
4
5/// Result type alias for polars-redis operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur in polars-redis operations.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Redis connection error.
12    #[error("Redis connection error: {0}. Check that Redis is running and accessible.")]
13    Connection(#[from] redis::RedisError),
14
15    /// Invalid connection URL.
16    #[error("Invalid Redis URL '{0}'. Expected format: redis://[user:password@]host[:port][/db]")]
17    InvalidUrl(String),
18
19    /// Schema mismatch error.
20    #[error("Schema mismatch: {0}. Ensure the schema matches the Redis data structure.")]
21    SchemaMismatch(String),
22
23    /// Type conversion error.
24    #[error("Type conversion error: {0}. Check that the schema types match the actual data.")]
25    TypeConversion(String),
26
27    /// IO error.
28    #[error("IO error: {0}")]
29    Io(#[from] std::io::Error),
30
31    /// Tokio runtime error.
32    #[error("Runtime error: {0}")]
33    Runtime(String),
34
35    /// Key not found error.
36    #[error("Key not found: {0}")]
37    KeyNotFound(String),
38
39    /// RedisJSON module not available.
40    #[error("RedisJSON module not available. Install redis-stack or load the ReJSON module.")]
41    JsonModuleNotAvailable,
42
43    /// Invalid input parameter.
44    #[error("Invalid input: {0}")]
45    InvalidInput(String),
46
47    /// Key already exists (when using WriteMode::Fail).
48    #[error("Key already exists: {0}")]
49    KeyExists(String),
50
51    /// Channel communication error.
52    #[error("Channel error: {0}")]
53    Channel(String),
54}
55
56#[cfg(feature = "python")]
57impl From<Error> for pyo3::PyErr {
58    fn from(err: Error) -> pyo3::PyErr {
59        match err {
60            Error::Connection(_) | Error::InvalidUrl(_) => {
61                pyo3::exceptions::PyConnectionError::new_err(err.to_string())
62            }
63            Error::SchemaMismatch(_) | Error::TypeConversion(_) => {
64                pyo3::exceptions::PyValueError::new_err(err.to_string())
65            }
66            Error::Io(_) => pyo3::exceptions::PyIOError::new_err(err.to_string()),
67            Error::Runtime(_) => pyo3::exceptions::PyRuntimeError::new_err(err.to_string()),
68            Error::KeyNotFound(_) => pyo3::exceptions::PyKeyError::new_err(err.to_string()),
69            Error::JsonModuleNotAvailable => {
70                pyo3::exceptions::PyRuntimeError::new_err(err.to_string())
71            }
72            Error::InvalidInput(_) => pyo3::exceptions::PyValueError::new_err(err.to_string()),
73            Error::KeyExists(_) => pyo3::exceptions::PyValueError::new_err(err.to_string()),
74            Error::Channel(_) => pyo3::exceptions::PyRuntimeError::new_err(err.to_string()),
75        }
76    }
77}