22
33#![ deny( missing_docs) ]
44
5- use failure:: { format_err, Fail } ;
65use jsonrpc_core:: futures:: channel:: { mpsc, oneshot} ;
76use jsonrpc_core:: futures:: {
87 self ,
@@ -22,20 +21,34 @@ pub mod transports;
2221mod logger;
2322
2423/// The errors returned by the client.
25- #[ derive( Debug , Fail ) ]
24+ #[ derive( Debug , derive_more :: Display ) ]
2625pub enum RpcError {
2726 /// An error returned by the server.
28- #[ fail ( display = "Server returned rpc error {}" , _0) ]
27+ #[ display( fmt = "Server returned rpc error {}" , _0) ]
2928 JsonRpcError ( Error ) ,
3029 /// Failure to parse server response.
31- #[ fail ( display = "Failed to parse server response as {}: {}" , _0, _1) ]
32- ParseError ( String , failure :: Error ) ,
30+ #[ display( fmt = "Failed to parse server response as {}: {}" , _0, _1) ]
31+ ParseError ( String , Box < dyn std :: error :: Error + Send > ) ,
3332 /// Request timed out.
34- #[ fail ( display = "Request timed out" ) ]
33+ #[ display( fmt = "Request timed out" ) ]
3534 Timeout ,
35+ /// A general client error.
36+ #[ display( fmt = "Client error: {}" , _0) ]
37+ Client ( String ) ,
3638 /// Not rpc specific errors.
37- #[ fail( display = "{}" , _0) ]
38- Other ( failure:: Error ) ,
39+ #[ display( fmt = "{}" , _0) ]
40+ Other ( Box < dyn std:: error:: Error + Send > ) ,
41+ }
42+
43+ impl std:: error:: Error for RpcError {
44+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
45+ match * self {
46+ Self :: JsonRpcError ( ref e) => Some ( e) ,
47+ Self :: ParseError ( _, ref e) => Some ( & * * e) ,
48+ Self :: Other ( ref e) => Some ( & * * e) ,
49+ _ => None ,
50+ }
51+ }
3952}
4053
4154impl From < Error > for RpcError {
@@ -162,7 +175,7 @@ impl<T: DeserializeOwned + Unpin + 'static> Stream for TypedSubscriptionStream<T
162175 match result {
163176 Some ( Ok ( value) ) => Some (
164177 serde_json:: from_value :: < T > ( value)
165- . map_err ( |error| RpcError :: ParseError ( self . returns . into ( ) , error . into ( ) ) ) ,
178+ . map_err ( |error| RpcError :: ParseError ( self . returns . into ( ) , Box :: new ( error ) ) ) ,
166179 ) ,
167180 None => None ,
168181 Some ( Err ( err) ) => Some ( Err ( err. into ( ) ) ) ,
@@ -192,9 +205,9 @@ impl RawClient {
192205 } ;
193206 let result = self . 0 . send ( msg. into ( ) ) ;
194207 async move {
195- let ( ) = result. map_err ( |e| RpcError :: Other ( e . into ( ) ) ) ?;
208+ let ( ) = result. map_err ( |e| RpcError :: Other ( Box :: new ( e ) ) ) ?;
196209
197- receiver. await . map_err ( |e| RpcError :: Other ( e . into ( ) ) ) ?
210+ receiver. await . map_err ( |e| RpcError :: Other ( Box :: new ( e ) ) ) ?
198211 }
199212 }
200213
@@ -206,7 +219,7 @@ impl RawClient {
206219 } ;
207220 match self . 0 . send ( msg. into ( ) ) {
208221 Ok ( ( ) ) => Ok ( ( ) ) ,
209- Err ( error) => Err ( RpcError :: Other ( error . into ( ) ) ) ,
222+ Err ( error) => Err ( RpcError :: Other ( Box :: new ( error ) ) ) ,
210223 }
211224 }
212225
@@ -232,7 +245,7 @@ impl RawClient {
232245 self . 0
233246 . send ( msg. into ( ) )
234247 . map ( |( ) | receiver)
235- . map_err ( |e| RpcError :: Other ( e . into ( ) ) )
248+ . map_err ( |e| RpcError :: Other ( Box :: new ( e ) ) )
236249 }
237250}
238251
@@ -266,9 +279,9 @@ impl TypedClient {
266279 Value :: Array ( vec) => Ok ( Params :: Array ( vec) ) ,
267280 Value :: Null => Ok ( Params :: None ) ,
268281 Value :: Object ( map) => Ok ( Params :: Map ( map) ) ,
269- _ => Err ( RpcError :: Other ( format_err ! (
270- "RPC params should serialize to a JSON array, JSON object or null"
271- ) ) ) ,
282+ _ => Err ( RpcError :: Client (
283+ "RPC params should serialize to a JSON array, JSON object or null" . into ( ) ,
284+ ) ) ,
272285 } ;
273286 let result = params. map ( |params| self . 0 . call_method ( method, params) ) ;
274287
@@ -277,7 +290,7 @@ impl TypedClient {
277290
278291 log:: debug!( "response: {:?}" , value) ;
279292
280- serde_json:: from_value :: < R > ( value) . map_err ( |error| RpcError :: ParseError ( returns, error . into ( ) ) )
293+ serde_json:: from_value :: < R > ( value) . map_err ( |error| RpcError :: ParseError ( returns, Box :: new ( error ) ) )
281294 }
282295 }
283296
@@ -289,9 +302,9 @@ impl TypedClient {
289302 Value :: Array ( vec) => Params :: Array ( vec) ,
290303 Value :: Null => Params :: None ,
291304 _ => {
292- return Err ( RpcError :: Other ( format_err ! (
293- "RPC params should serialize to a JSON array, or null"
294- ) ) )
305+ return Err ( RpcError :: Client (
306+ "RPC params should serialize to a JSON array, or null" . into ( ) ,
307+ ) )
295308 }
296309 } ;
297310
@@ -314,9 +327,9 @@ impl TypedClient {
314327 Value :: Array ( vec) => Params :: Array ( vec) ,
315328 Value :: Null => Params :: None ,
316329 _ => {
317- return Err ( RpcError :: Other ( format_err ! (
318- "RPC params should serialize to a JSON array, or null"
319- ) ) )
330+ return Err ( RpcError :: Client (
331+ "RPC params should serialize to a JSON array, or null" . into ( ) ,
332+ ) )
320333 }
321334 } ;
322335
0 commit comments