Improve error messages

This commit is contained in:
htrefil 2020-10-29 17:50:55 +01:00
parent 9ca2c8c798
commit 3b9f15f529
5 changed files with 29 additions and 25 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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<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)
.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<Infalli
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
));
}
@ -89,7 +89,7 @@ async fn main() {
tokio::select! {
result = run(&config.server.hostname, config.server.port, &config.certificate_path) => {
if let Err(err) = result {
log::error!("Error: {}", err);
log::error!("Error: {:#}", err);
process::exit(1);
}
}

View file

@ -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"

View file

@ -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<Infallible, Error> {
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);
}
}