1use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum Error {
8 #[error("Connection error: {0}")]
10 Connection(String),
11
12 #[error("Protocol error: {0}")]
14 Protocol(String),
15
16 #[error("Server error: {0}")]
18 Server(String),
19
20 #[error("Operation timed out: {0}")]
22 Timeout(#[from] tokio::time::error::Elapsed),
23
24 #[error("I/O error: {0}")]
26 Io(#[from] std::io::Error),
27
28 #[error("Serialization error: {0}")]
30 Serialization(String),
31
32 #[error("Invalid argument: {0}")]
34 InvalidArgument(String),
35
36 #[error("Key not found")]
38 KeyNotFound,
39
40 #[error("Connection pool exhausted")]
42 PoolExhausted,
43
44 #[error("Invalid response: {0}")]
46 InvalidResponse(String),
47
48 #[error("Authentication failed")]
50 AuthenticationFailed,
51
52 #[error("Not connected")]
54 NotConnected,
55
56 #[error("Operation not supported")]
58 NotSupported,
59
60 #[error("JSON error: {0}")]
62 Json(#[from] serde_json::Error),
63
64 #[error("{0}")]
66 Other(String),
67}
68
69impl Error {
70 pub fn connection<S: Into<String>>(msg: S) -> Self {
72 Error::Connection(msg.into())
73 }
74
75 pub fn protocol<S: Into<String>>(msg: S) -> Self {
77 Error::Protocol(msg.into())
78 }
79
80 pub fn server<S: Into<String>>(msg: S) -> Self {
82 Error::Server(msg.into())
83 }
84
85 pub fn invalid_argument<S: Into<String>>(msg: S) -> Self {
87 Error::InvalidArgument(msg.into())
88 }
89
90 pub fn invalid_response<S: Into<String>>(msg: S) -> Self {
92 Error::InvalidResponse(msg.into())
93 }
94
95 pub fn other<S: Into<String>>(msg: S) -> Self {
97 Error::Other(msg.into())
98 }
99}
100
101impl From<String> for Error {
102 fn from(s: String) -> Self {
103 Error::Other(s)
104 }
105}
106
107impl From<&str> for Error {
108 fn from(s: &str) -> Self {
109 Error::Other(s.to_string())
110 }
111}