Skip to content

Commit efdc7d2

Browse files
committed
Improving error type
1 parent aacb129 commit efdc7d2

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/types.rs

Lines changed: 44 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

@@ -264,6 +265,7 @@ pub struct ScriptNotification {
264265

265266
/// Errors
266267
#[derive(Debug)]
268+
#[non_exhaustive]
267269
pub enum Error {
268270
/// Wraps `std::io::Error`
269271
IOError(std::io::Error),
@@ -291,7 +293,7 @@ pub enum Error {
291293
AllAttemptsErrored(Vec<Error>),
292294
/// There was an io error reading the socket, to be shared between threads
293295
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
295297
BothSocksAndTimeout,
296298
/// Setting both a timeout and passing zero or more than one socket addrs is an error
297299
WrongAddrsNumberWithTimeout,
@@ -308,11 +310,51 @@ pub enum Error {
308310
SslHandshakeError(openssl::ssl::HandshakeError<std::net::TcpStream>),
309311
}
310312

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+
311353
macro_rules! impl_error {
312354
( $from:ty, $to:ident ) => {
313355
impl std::convert::From<$from> for Error {
314356
fn from(err: $from) -> Self {
315-
Error::$to(err)
357+
Error::$to(err.into())
316358
}
317359
}
318360
};

0 commit comments

Comments
 (0)