From 727cd53eae672d342efc7bbded1b898b2e1fc5c4 Mon Sep 17 00:00:00 2001 From: htrefil <8711792+htrefil@users.noreply.github.com> Date: Thu, 29 Oct 2020 17:50:55 +0100 Subject: [PATCH] Improve error messages --- Cargo.lock | 2 ++ client/Cargo.toml | 1 + client/src/main.rs | 26 +++++++++++++------------- server/Cargo.toml | 1 + server/src/main.rs | 24 ++++++++++++------------ 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d2f45e..acb3283 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -159,6 +159,7 @@ dependencies = [ name = "client" version = "0.1.0" dependencies = [ + "anyhow", "env_logger 0.8.1", "input", "log", @@ -659,6 +660,7 @@ dependencies = [ name = "server" version = "0.1.0" dependencies = [ + "anyhow", "env_logger 0.8.1", "input", "log", diff --git a/client/Cargo.toml b/client/Cargo.toml index f98a717..d9e0b8a 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -16,3 +16,4 @@ structopt = "0.3.20" log = "0.4.11" env_logger = "0.8.1" tokio-native-tls = "0.2.0" +anyhow = "1.0.33" diff --git a/client/src/main.rs b/client/src/main.rs index df5d2c1..81a58ac 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,10 +1,10 @@ mod config; +use anyhow::{Context, Error}; use config::Config; use input::EventWriter; use net::{self, Message, PROTOCOL_VERSION}; use std::convert::Infallible; -use std::io::{Error, ErrorKind}; use std::path::{Path, PathBuf}; use std::process; use structopt::StructOpt; @@ -13,22 +13,24 @@ use tokio::net::TcpStream; use tokio_native_tls::native_tls::{Certificate, TlsConnector}; async fn run(server: &str, port: u16, certificate_path: &Path) -> Result { - let certificate = fs::read(certificate_path).await?; + let certificate = fs::read(certificate_path) + .await + .context("Failed to read certificate")?; let certificate = Certificate::from_der(&certificate) .or_else(|_| Certificate::from_pem(&certificate)) - .map_err(|err| Error::new(ErrorKind::InvalidData, err))?; + .context("Failed to parse certificate")?; let connector: tokio_native_tls::TlsConnector = TlsConnector::builder() .add_root_certificate(certificate) .build() - .map_err(|err| Error::new(ErrorKind::InvalidData, err)) - .map(Into::into)?; + .context("Failed to create connector")? + .into(); let stream = TcpStream::connect((server, port)).await?; let mut stream = connector .connect(server, stream) .await - .map_err(|err| Error::new(ErrorKind::InvalidData, err))?; + .context("Failed to connect")?; log::info!("Connected to {}:{}", server, port); @@ -36,12 +38,10 @@ async fn run(server: &str, port: u16, certificate_path: &Path) -> Result { if let Err(err) = result { - log::error!("Error: {}", err); + log::error!("Error: {:#}", err); process::exit(1); } } diff --git a/server/Cargo.toml b/server/Cargo.toml index e821392..ef7b895 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -16,3 +16,4 @@ structopt = "0.3.20" log = "0.4.11" env_logger = "0.8.1" tokio-native-tls = "0.2.0" +anyhow = "1.0.33" diff --git a/server/src/main.rs b/server/src/main.rs index b5ecb1a..bc08edd 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,11 +1,11 @@ mod config; +use anyhow::{Context, Error}; use config::Config; use input::{Direction, Event, EventManager}; use net::{self, Message, PROTOCOL_VERSION}; use std::collections::{HashMap, HashSet}; use std::convert::Infallible; -use std::io::{Error, ErrorKind}; use std::net::SocketAddr; use std::path::{Path, PathBuf}; use std::process; @@ -29,12 +29,10 @@ where let version = net::read_version(&mut stream).await?; if version != PROTOCOL_VERSION { - return Err(Error::new( - ErrorKind::InvalidData, - format!( - "Incompatible protocol version (got {}, expecting {})", - version, PROTOCOL_VERSION - ), + return Err(anyhow::anyhow!( + "Incompatible protocol version (got {}, expecting {})", + version, + PROTOCOL_VERSION )); } @@ -55,11 +53,13 @@ async fn run( identity_path: &Path, identity_password: &str, ) -> Result { - let identity = fs::read(identity_path).await?; - let identity = Identity::from_pkcs12(&identity, identity_password) - .map_err(|err| Error::new(ErrorKind::InvalidData, err))?; + let identity = fs::read(identity_path) + .await + .context("Failed to read identity")?; + let identity = + Identity::from_pkcs12(&identity, identity_password).context("Failed to parse identity")?; let acceptor: tokio_native_tls::TlsAcceptor = TlsAcceptor::new(identity) - .map_err(|err| Error::new(ErrorKind::InvalidData, err)) + .context("Failed to create TLS acceptor") .map(Into::into)?; let listener = TcpListener::bind(listen_address).await?; @@ -185,7 +185,7 @@ async fn main() { tokio::select! { result = run(config.listen_address, &config.switch_keys, &config.identity_path, &config.identity_password) => { if let Err(err) = result { - log::error!("Error: {}", err); + log::error!("Error: {:#}", err); process::exit(1); } }