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