1use std::fmt;
2use std::error::Error;
3
4#[derive(Debug, Clone, PartialEq)]
5pub enum RedisErrorKind {
6 InternalError,
7 IncorrectConversion,
8 ConnectionError,
9 ParseError,
10 ReceiveError,
11 InvalidOptions,
12}
13
14#[derive(Debug, Clone)]
15pub struct RedisError {
16 pub error: RedisErrorKind,
17 desc: String,
18}
19
20pub type RedisResult<T> = Result<T, RedisError>;
21
22impl RedisError {
23 pub fn new(error: RedisErrorKind, desc: String) -> RedisError {
24 RedisError { error, desc }
25 }
26}
27
28impl fmt::Display for RedisError {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(f, "error: \"{}\", description: \"{}\"",
31 to_string(&self.error),
32 &self.desc)
33 }
34}
35
36impl std::error::Error for RedisError {
37 fn description(&self) -> &str {
38 &self.desc
39 }
40}
41
42impl From<std::io::Error> for RedisError {
43 fn from(err: std::io::Error) -> Self {
44 RedisError { error: RedisErrorKind::ConnectionError, desc: err.description().to_string() }
45 }
46}
47
48fn to_string(err: &RedisErrorKind) -> &'static str {
49 match err {
50 RedisErrorKind::InternalError => "InternalError",
51 RedisErrorKind::IncorrectConversion => "IncorrectConversion",
52 RedisErrorKind::ConnectionError => "ConnectionError",
53 RedisErrorKind::ParseError => "ParseError",
54 RedisErrorKind::ReceiveError => "ReceiveError",
55 RedisErrorKind::InvalidOptions => "InvalidOptions",
56 }
57}