mco_redis/
errors.rs

1//! Redis protocol related errors
2use std::io;
3
4use derive_more::{Display, From};
5use crate::bytes::ByteString;
6use super::codec_redis::Response;
7
8#[derive(Debug, Display)]
9/// Redis protocol errors
10pub enum Error {
11    /// A RESP parsing error occurred
12    #[display(fmt = "Redis server response error: {}", _0)]
13    Parse(String),
14
15    /// An IO error occurred
16    #[display(fmt = "Io error: {:?}", _0)]
17    PeerGone(Option<io::Error>),
18
19    #[display(fmt = "Command error: {:?}", _0)]
20    Command(String),
21
22    #[display(fmt = "Recv error: {:?}", _0)]
23    Recv(String),
24}
25
26impl std::error::Error for Error {}
27
28impl Clone for Error {
29    fn clone(&self) -> Self {
30        match self {
31            Error::Parse(s) => Error::Parse(s.clone()),
32            Error::PeerGone(_) => Error::PeerGone(None),
33            Error::Command(v)=> Error::Command(v.clone()),
34            Error::Recv(v)=> Error::Recv(v.clone()),
35        }
36    }
37}
38
39impl From<io::Error> for Error {
40    fn from(err: io::Error) -> Error {
41        Error::PeerGone(Some(err))
42    }
43}
44
45impl From<CommandError> for Error{
46    fn from(arg: CommandError) -> Self {
47        Error::Command(format!("{:?}",arg))
48    }
49}
50
51#[derive(Debug, Display, From)]
52/// Redis connectivity errors
53pub enum ConnectError {
54    /// Auth command failed
55    Unauthorized,
56    /// Command execution error
57    Command(CommandError),
58    /// Io connectivity error
59    Connect(String),
60}
61
62impl std::error::Error for ConnectError {}
63
64impl From<std::io::Error> for ConnectError {
65    fn from(arg: std::io::Error) -> Self {
66        ConnectError::Connect(arg.to_string())
67    }
68}
69
70#[derive(Debug, Display, From)]
71/// Redis command execution errors
72pub enum CommandError {
73    /// A redis server error response
74    Error(ByteString),
75
76    /// A command response parse error
77    #[display(fmt = "Command output parse error: {}", _0)]
78    Output(&'static str, Response),
79
80    /// Redis protocol level errors
81    Protocol(Error),
82}
83
84impl std::error::Error for CommandError {}
85
86impl From<std::io::Error> for CommandError{
87    fn from(arg: io::Error) -> Self {
88        CommandError::Protocol(Error::PeerGone(Some(arg)))
89    }
90}