1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pub use crate::raw;
use std::ffi::CStr;
use std::fmt;
#[derive(Debug)]
pub enum RedisError {
WrongArity,
Str(&'static str),
String(String),
WrongType,
}
impl RedisError {
pub fn nonexistent_key() -> Self {
RedisError::Str("ERR could not perform this operation on a key that doesn't exist")
}
}
impl<T: std::error::Error> From<T> for RedisError {
fn from(e: T) -> Self {
RedisError::String(format!("ERR {}", e))
}
}
impl fmt::Display for RedisError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let d = match self {
RedisError::WrongArity => "Wrong Arity",
RedisError::WrongType => std::str::from_utf8(
CStr::from_bytes_with_nul(raw::REDISMODULE_ERRORMSG_WRONGTYPE)
.unwrap()
.to_bytes(),
)
.unwrap(),
RedisError::Str(s) => s,
RedisError::String(s) => s.as_str(),
};
write!(f, "{}", d)
}
}