11//! Custom error handling for Redis client and a specialized Result type
22//! used as the return type for Redis operations.
3+ //!
4+ //! todo: implement From trait for RedisError so that we can capture more built in e
35
4- use std:: { error, fmt, io, result, sync } ;
6+ use std:: { error, fmt, io, result} ;
57
68/// Represents errors that can occur when working with Redis.
79#[ derive( Debug ) ]
810pub enum RedisError {
9- /// An I/O error that occurred while working with a Redis connection.
10- Io ( io:: Error ) ,
1111 /// An incomplete frame was received when reading from the socket.
1212 IncompleteFrame ,
1313 /// An invalid frame was received when reading from the socket. According to RESP3 spec.
1414 InvalidFrame ,
15- Other ( String ) ,
16- }
17-
18- impl From < io:: Error > for RedisError {
19- fn from ( err : io:: Error ) -> Self {
20- RedisError :: Io ( err)
21- }
15+ /// Generic error type.
16+ Other ( Error ) ,
2217}
2318
2419impl fmt:: Display for RedisError {
2520 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2621 match self {
27- RedisError :: Io ( err) => write ! ( f, "IO error: {}" , err) ,
2822 RedisError :: IncompleteFrame => write ! ( f, "incomplete frame" ) ,
2923 RedisError :: InvalidFrame => write ! ( f, "invalid frame" ) ,
3024 RedisError :: Other ( s) => write ! ( f, "other error: {}" , s) ,
@@ -35,12 +29,31 @@ impl fmt::Display for RedisError {
3529// Implement std::error::Error for RedisError.
3630impl error:: Error for RedisError { }
3731
38- type Error = sync:: Arc < RedisError > ;
32+ impl From < io:: Error > for RedisError {
33+ fn from ( err : io:: Error ) -> Self {
34+ RedisError :: Other ( err. into ( ) )
35+ }
36+ }
37+
38+ impl From < String > for RedisError {
39+ fn from ( val : String ) -> Self {
40+ RedisError :: Other ( val. into ( ) )
41+ }
42+ }
3943
40- /// Helper function to wrap errors into Arc.
41- pub fn wrap_error < E : Into < RedisError > > ( err : E ) -> Error {
42- sync:: Arc :: new ( err. into ( ) )
44+ impl From < & str > for RedisError {
45+ fn from ( val : & str ) -> Self {
46+ RedisError :: Other ( val. into ( ) )
47+ }
4348}
4449
50+ /// Boxed generic error types.
51+ type Error = Box < dyn std:: error:: Error + Send + Sync > ;
52+
4553/// A specialized `Result` type for Redis operations.
4654pub type Result < T > = result:: Result < T , Error > ;
55+
56+ /// Helper function to wrap errors into Box.
57+ pub fn wrap_error < E : Into < RedisError > > ( err : E ) -> Error {
58+ Box :: new ( err. into ( ) )
59+ }
0 commit comments