1use redis::RedisError;
2
3#[derive(Debug, PartialEq)]
5pub enum RdLockError {
6 RedisError(RedisError),
8 LockUnavailable,
10 TimeoutReached,
12 LockNotAcquired,
14 ConnectionError,
16 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}