From d01bb57a83b6dbef79e93639f784ee2ff6b72bf4 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 22 Jan 2018 14:59:34 +0100 Subject: Use the failure crate to handle errors. - The failure crate is a young error handling solution for Rust. It may change the API, but since we pin our dependencies, this should not be a problem for us, albeit a bit inconvenient. - Introduction of the crate is a bit noisy, but not as bad as anticipated, because failure magically handles all errors used in the standard library. - Matching on concrete error values requires downcasting before matching, which seems a bit unidiomatic. This is the cost of using and "chaining" arbitrary error types. This is something that may be improved later on in the library or language. - Having said that, using the error type in the tool was nice. I did not have to use a downcast, so maybe my worries about downcasts are unjustified because it is not such a common use case after all. On the other hand, the tool is quite simple and our only mode of failure is to print the message. --- net/Cargo.toml | 1 + net/src/lib.rs | 87 ++++++++++++++++------------------------------------------ 2 files changed, 25 insertions(+), 63 deletions(-) (limited to 'net') diff --git a/net/Cargo.toml b/net/Cargo.toml index 91775697..06a3ad72 100644 --- a/net/Cargo.toml +++ b/net/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Justus Winter "] openpgp = { path = "../openpgp" } sequoia-core = { path = "../core" } +failure = "0.1.1" fs2 = "0.4.2" futures = "0.1" hyper = "0.11" diff --git a/net/src/lib.rs b/net/src/lib.rs index 0e5497b5..52a43a60 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -33,6 +33,8 @@ extern crate openpgp; extern crate sequoia_core; +#[macro_use] +extern crate failure; extern crate futures; extern crate hyper; extern crate hyper_tls; @@ -54,10 +56,9 @@ use self::native_tls::{Certificate, TlsConnector}; use self::tokio_core::reactor::Core; use std::convert::From; use std::io::{Cursor, Read}; -use std::io; use sequoia_core::{Context, NetworkPolicy}; -use openpgp::tpk::{self, TPK}; +use openpgp::tpk::TPK; use openpgp::{Message, KeyID, armor}; pub mod ipc; @@ -93,7 +94,7 @@ impl KeyServer { &core.handle())?) .build(&core.handle())) }, - _ => return Err(Error::MalformedUri), + _ => return Err(Error::MalformedUri.into()), }; Self::make(ctx, core, client, uri) @@ -174,14 +175,14 @@ impl KeyServer { r.read_to_end(&mut key)?; Ok(key) }, - StatusCode::NotFound => Err(Error::NotFound), - n => Err(Error::from(n)), + StatusCode::NotFound => Err(Error::NotFound.into()), + n => Err(Error::HttpStatus(n).into()), } - Err(e) => Err(Error::HyperError(e)), + Err(e) => Err(Error::HyperError(e).into()), }; let m = Message::from_bytes(&key?)?; - TPK::from_message(m).map_err(|e| Error::KeysError(e)) + TPK::from_message(m) } /// Sends the given key to the server. @@ -216,10 +217,10 @@ impl KeyServer { Ok((status, _body)) => match status { StatusCode::Ok => Ok(()), - StatusCode::NotFound => Err(Error::ProtocolViolation), - n => Err(Error::from(n)), + StatusCode::NotFound => Err(Error::ProtocolViolation.into()), + n => Err(Error::HttpStatus(n).into()), } - Err(e) => Err(Error::HyperError(e)), + Err(e) => Err(Error::HyperError(e).into()), } } } @@ -248,73 +249,33 @@ impl AClient for Client> { } /// Results for sequoia-net. -pub type Result = ::std::result::Result; +pub type Result = ::std::result::Result; +#[derive(Fail, Debug)] /// Errors returned from the network routines. -#[derive(Debug)] pub enum Error { /// A requested key was not found. + #[fail(display = "Key not found")] NotFound, /// A given keyserver URI was malformed. + #[fail(display = "Malformed URI")] MalformedUri, /// The server provided malformed data. + #[fail(display = "Malformed response from server")] MalformedResponse, /// A communication partner violated the protocol. + #[fail(display = "Protocol violation")] ProtocolViolation, - /// There was an error parsing the key. - KeysError(tpk::Error), - /// A `sequoia_core::Error` occured. - CoreError(sequoia_core::Error), /// Encountered an unexpected low-level http status. + #[fail(display = "Error communicating with server")] HttpStatus(hyper::StatusCode), - /// An `io::Error` occured. - IoError(io::Error), - /// A `hyper::error::UriError` occured. + /// A `hyper::error::UriError` occurred. + #[fail(display = "URI Error")] UriError(hyper::error::UriError), - /// A `hyper::Error` occured. + /// A `hyper::Error` occurred. + #[fail(display = "Hyper Error")] HyperError(hyper::Error), - /// A `native_tls::Error` occured. + /// A `native_tls::Error` occurred. + #[fail(display = "TLS Error")] TlsError(native_tls::Error), } - -impl From for Error { - fn from(e: tpk::Error) -> Self { - Error::KeysError(e) - } -} - -impl From for Error { - fn from(e: sequoia_core::Error) -> Self { - Error::CoreError(e) - } -} - -impl From for Error { - fn from(status: hyper::StatusCode) -> Self { - Error::HttpStatus(status) - } -} - -impl From for Error { - fn from(error: io::Error) -> Self { - Error::IoError(error) - } -} - -impl From for Error { - fn from(error: hyper::Error) -> Self { - Error::HyperError(error) - } -} - -impl From for Error { - fn from(error: hyper::error::UriError) -> Self { - Error::UriError(error) - } -} - -impl From for Error { - fn from(error: native_tls::Error) -> Self { - Error::TlsError(error) - } -} -- cgit v1.2.3