Improve error messages

This commit is contained in:
htrefil 2020-10-29 17:50:55 +01:00
parent dfde202a7b
commit 727cd53eae
5 changed files with 29 additions and 25 deletions

2
Cargo.lock generated
View file

@ -159,6 +159,7 @@ dependencies = [
name = "client" name = "client"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"env_logger 0.8.1", "env_logger 0.8.1",
"input", "input",
"log", "log",
@ -659,6 +660,7 @@ dependencies = [
name = "server" name = "server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"env_logger 0.8.1", "env_logger 0.8.1",
"input", "input",
"log", "log",

View file

@ -16,3 +16,4 @@ structopt = "0.3.20"
log = "0.4.11" log = "0.4.11"
env_logger = "0.8.1" env_logger = "0.8.1"
tokio-native-tls = "0.2.0" tokio-native-tls = "0.2.0"
anyhow = "1.0.33"

View file

@ -1,10 +1,10 @@
mod config; mod config;
use anyhow::{Context, Error};
use config::Config; use config::Config;
use input::EventWriter; use input::EventWriter;
use net::{self, Message, PROTOCOL_VERSION}; use net::{self, Message, PROTOCOL_VERSION};
use std::convert::Infallible; use std::convert::Infallible;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process; use std::process;
use structopt::StructOpt; use structopt::StructOpt;
@ -13,22 +13,24 @@ use tokio::net::TcpStream;
use tokio_native_tls::native_tls::{Certificate, TlsConnector}; use tokio_native_tls::native_tls::{Certificate, TlsConnector};
async fn run(server: &str, port: u16, certificate_path: &Path) -> Result<Infallible, Error> { async fn run(server: &str, port: u16, certificate_path: &Path) -> Result<Infallible, Error> {
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) let certificate = Certificate::from_der(&certificate)
.or_else(|_| Certificate::from_pem(&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() let connector: tokio_native_tls::TlsConnector = TlsConnector::builder()
.add_root_certificate(certificate) .add_root_certificate(certificate)
.build() .build()
.map_err(|err| Error::new(ErrorKind::InvalidData, err)) .context("Failed to create connector")?
.map(Into::into)?; .into();
let stream = TcpStream::connect((server, port)).await?; let stream = TcpStream::connect((server, port)).await?;
let mut stream = connector let mut stream = connector
.connect(server, stream) .connect(server, stream)
.await .await
.map_err(|err| Error::new(ErrorKind::InvalidData, err))?; .context("Failed to connect")?;
log::info!("Connected to {}:{}", server, port); log::info!("Connected to {}:{}", server, port);
@ -36,12 +38,10 @@ async fn run(server: &str, port: u16, certificate_path: &Path) -> Result<Infalli
let version = net::read_version(&mut stream).await?; let version = net::read_version(&mut stream).await?;
if version != PROTOCOL_VERSION { if version != PROTOCOL_VERSION {
return Err(Error::new( return Err(anyhow::anyhow!(
ErrorKind::InvalidData, "Incompatible protocol version (got {}, expecting {})",
format!( version,
"Incompatible protocol version (got {}, expecting {})", PROTOCOL_VERSION
version, PROTOCOL_VERSION
),
)); ));
} }
@ -89,7 +89,7 @@ async fn main() {
tokio::select! { tokio::select! {
result = run(&config.server.hostname, config.server.port, &config.certificate_path) => { result = run(&config.server.hostname, config.server.port, &config.certificate_path) => {
if let Err(err) = result { if let Err(err) = result {
log::error!("Error: {}", err); log::error!("Error: {:#}", err);
process::exit(1); process::exit(1);
} }
} }

View file

@ -16,3 +16,4 @@ structopt = "0.3.20"
log = "0.4.11" log = "0.4.11"
env_logger = "0.8.1" env_logger = "0.8.1"
tokio-native-tls = "0.2.0" tokio-native-tls = "0.2.0"
anyhow = "1.0.33"

View file

@ -1,11 +1,11 @@
mod config; mod config;
use anyhow::{Context, Error};
use config::Config; use config::Config;
use input::{Direction, Event, EventManager}; use input::{Direction, Event, EventManager};
use net::{self, Message, PROTOCOL_VERSION}; use net::{self, Message, PROTOCOL_VERSION};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::convert::Infallible; use std::convert::Infallible;
use std::io::{Error, ErrorKind};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process; use std::process;
@ -29,12 +29,10 @@ where
let version = net::read_version(&mut stream).await?; let version = net::read_version(&mut stream).await?;
if version != PROTOCOL_VERSION { if version != PROTOCOL_VERSION {
return Err(Error::new( return Err(anyhow::anyhow!(
ErrorKind::InvalidData, "Incompatible protocol version (got {}, expecting {})",
format!( version,
"Incompatible protocol version (got {}, expecting {})", PROTOCOL_VERSION
version, PROTOCOL_VERSION
),
)); ));
} }
@ -55,11 +53,13 @@ async fn run(
identity_path: &Path, identity_path: &Path,
identity_password: &str, identity_password: &str,
) -> Result<Infallible, Error> { ) -> Result<Infallible, Error> {
let identity = fs::read(identity_path).await?; let identity = fs::read(identity_path)
let identity = Identity::from_pkcs12(&identity, identity_password) .await
.map_err(|err| Error::new(ErrorKind::InvalidData, err))?; .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) 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)?; .map(Into::into)?;
let listener = TcpListener::bind(listen_address).await?; let listener = TcpListener::bind(listen_address).await?;
@ -185,7 +185,7 @@ async fn main() {
tokio::select! { tokio::select! {
result = run(config.listen_address, &config.switch_keys, &config.identity_path, &config.identity_password) => { result = run(config.listen_address, &config.switch_keys, &config.identity_path, &config.identity_password) => {
if let Err(err) = result { if let Err(err) = result {
log::error!("Error: {}", err); log::error!("Error: {:#}", err);
process::exit(1); process::exit(1);
} }
} }