rdlock/
errors.rs

1use redis::RedisError;
2
3/// Errors returned from various `rdlock` functions
4#[derive(Debug, PartialEq)]
5pub enum RdLockError {
6    /// Error related to redis
7    RedisError(RedisError),
8    /// The lock is currently unavailable and could not be acquired
9    LockUnavailable,
10    /// Timeout has been reached
11    TimeoutReached,
12    /// The lock was not acquired by this instance
13    LockNotAcquired,
14    /// Connection error or error related to a redis command
15    ConnectionError,
16    /// The redis instance does not support distributed locks
17    InitCheckFailed,
18}
19
20impl From<RedisError> for RdLockError {
21    fn from(value: RedisError) -> Self {
22        Self::RedisError(value)
23    }
24}
25
26impl std::fmt::Display for RdLockError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        let msg = match self {
29            Self::RedisError(a) => format!("Redis error: {:?}", a),
30            Self::LockUnavailable => "LockUnavailable".to_string(),
31            Self::TimeoutReached => "TimeoutReached".to_string(),
32            Self::LockNotAcquired => "LockNotAcquired: The lock is currently not acquired".to_string(),
33            Self::ConnectionError => "ConnectionError".to_string(),
34            Self::InitCheckFailed => "InitCheckFailed: redis does not support distributed locks. Check README.md and make sure notify-keyspace-events is set".to_string()
35        };
36        write!(f, "{msg}")
37    }
38}
39
40impl std::error::Error for RdLockError {
41    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42        if let Self::RedisError(e) = self {
43            Some(e)
44        } else {
45            None
46        }
47    }
48}