3
3
//! This module contains definitions of all the complex data structures that are returned by calls
4
4
5
5
use std:: convert:: TryFrom ;
6
+ use std:: fmt:: { self , Display , Formatter } ;
6
7
use std:: ops:: Deref ;
7
8
use std:: sync:: Arc ;
8
9
@@ -291,7 +292,7 @@ pub enum Error {
291
292
AllAttemptsErrored ( Vec < Error > ) ,
292
293
/// There was an io error reading the socket, to be shared between threads
293
294
SharedIOError ( Arc < std:: io:: Error > ) ,
294
- /// Setting both a proxy and a timeout in `Config` results in this error
295
+ /// Setting both a proxy and a timeout in `Config` is an error
295
296
BothSocksAndTimeout ,
296
297
/// Setting both a timeout and passing zero or more than one socket addrs is an error
297
298
WrongAddrsNumberWithTimeout ,
@@ -308,11 +309,51 @@ pub enum Error {
308
309
SslHandshakeError ( openssl:: ssl:: HandshakeError < std:: net:: TcpStream > ) ,
309
310
}
310
311
312
+ impl Display for Error {
313
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
314
+ match self {
315
+ Error :: IOError ( e) => Display :: fmt ( e, f) ,
316
+ Error :: JSON ( e) => Display :: fmt ( e, f) ,
317
+ Error :: Hex ( e) => Display :: fmt ( e, f) ,
318
+ Error :: Bitcoin ( e) => Display :: fmt ( e, f) ,
319
+ Error :: SharedIOError ( e) => Display :: fmt ( e, f) ,
320
+ #[ cfg( feature = "use-openssl" ) ]
321
+ Error :: SslHandshakeError ( e) => Display :: fmt ( e, f) ,
322
+ #[ cfg( feature = "use-openssl" ) ]
323
+ Error :: InvalidSslMethod ( e) => Display :: fmt ( e, f) ,
324
+
325
+ Error :: Message ( e) => f. write_str ( e) ,
326
+ Error :: InvalidDNSNameError ( domain) => write ! ( f, "Invalid domain name {} not matching SSL certificate" , domain) ,
327
+ Error :: AllAttemptsErrored ( errors) => {
328
+ f. write_str ( "Made one or multiple attempts, all errored:\n " ) ?;
329
+ for err in errors {
330
+ writeln ! ( f, "\t - {}" , err) ?;
331
+ }
332
+ Ok ( ( ) )
333
+ }
334
+
335
+ Error :: Protocol ( e) => write ! ( f, "Electrum server error: {}" , e. clone( ) . take( ) ) ,
336
+ Error :: InvalidResponse ( e) => write ! ( f, "Error during the deserialization of a response from the server: {}" , e. clone( ) . take( ) ) ,
337
+
338
+ // TODO: Print out addresses once `ScriptHash` will implement `Display`
339
+ Error :: AlreadySubscribed ( _) => write ! ( f, "Already subscribed to the notifications of an address" ) ,
340
+ Error :: NotSubscribed ( _) => write ! ( f, "Not subscribed to the notifications of an address" ) ,
341
+
342
+ Error :: MissingDomain => f. write_str ( "Missing domain while it was explicitly asked to validate it" ) ,
343
+ Error :: BothSocksAndTimeout => f. write_str ( "Setting both a proxy and a timeout in `Config` is an error" ) ,
344
+ Error :: WrongAddrsNumberWithTimeout => f. write_str ( "Setting both a timeout and passing zero or more than one socket addrs is an error" ) ,
345
+ Error :: CouldntLockReader => f. write_str ( "Couldn't take a lock on the reader mutex. This means that there's already another reader thread is running" ) ,
346
+ }
347
+ }
348
+ }
349
+
350
+ impl std:: error:: Error for Error { }
351
+
311
352
macro_rules! impl_error {
312
353
( $from: ty, $to: ident ) => {
313
354
impl std:: convert:: From <$from> for Error {
314
355
fn from( err: $from) -> Self {
315
- Error :: $to( err)
356
+ Error :: $to( err. into ( ) )
316
357
}
317
358
}
318
359
} ;
0 commit comments