Skip to content

Commit 7db9a13

Browse files
committed
Merge commit 'refs/pull/37/head' of github.com:bitcoindevkit/rust-electrum-client
2 parents 1d59d96 + d927387 commit 7db9a13

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/types.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains definitions of all the complex data structures that are returned by calls
44
55
use std::convert::TryFrom;
6+
use std::fmt::{self, Display, Formatter};
67
use std::ops::Deref;
78
use std::sync::Arc;
89

@@ -291,7 +292,7 @@ pub enum Error {
291292
AllAttemptsErrored(Vec<Error>),
292293
/// There was an io error reading the socket, to be shared between threads
293294
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
295296
BothSocksAndTimeout,
296297
/// Setting both a timeout and passing zero or more than one socket addrs is an error
297298
WrongAddrsNumberWithTimeout,
@@ -308,11 +309,51 @@ pub enum Error {
308309
SslHandshakeError(openssl::ssl::HandshakeError<std::net::TcpStream>),
309310
}
310311

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+
311352
macro_rules! impl_error {
312353
( $from:ty, $to:ident ) => {
313354
impl std::convert::From<$from> for Error {
314355
fn from(err: $from) -> Self {
315-
Error::$to(err)
356+
Error::$to(err.into())
316357
}
317358
}
318359
};

0 commit comments

Comments
 (0)