redisesh/
error.rs

1use std::{fmt, str};
2
3#[derive(Debug)]
4pub enum Error {
5    DBConnectionError,
6    TokenCreationError,
7    RedisResponseError,
8}
9
10impl std::error::Error for Error {}
11
12impl fmt::Display for Error {
13    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14        match self {
15            Error::DBConnectionError => write!(f, "Error establishing connection with Redis"),
16            Error::TokenCreationError => write!(f, "Error while generating random token"),
17            Error::RedisResponseError => write!(f, "Redis response Error"),
18        }
19    }
20}
21
22impl From<redis::RedisError> for Error {
23    fn from(err: redis::RedisError) -> Self {
24        match err.kind() {
25            redis::ErrorKind::ResponseError => Error::RedisResponseError,
26            _ => Error::DBConnectionError,
27        }
28    }
29}
30
31impl From<str::Utf8Error> for Error {
32    fn from(_: str::Utf8Error) -> Self {
33        Error::TokenCreationError
34    }
35}