From 67da081ef9e5fba010c1b7407e4b3d91f25bce44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 12 Jun 2025 00:54:26 +0000 Subject: [PATCH] [wip] impl `Client` for `ureq::Agent` - error handling refactoring --- src/main.rs | 49 +++++++++++++++++++++++--------------------- src/sharry/client.rs | 18 +++++++++------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index d06addd..e9925b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,28 @@ use appstate::AppState; use cli::Cli; use sharry::ClientError; +fn print_error(e: &ClientError) { + if let Some(cause) = match e { + ClientError::ResponseStatus { + actual: 403, + expected: _, + } => Some("Alias ID"), + ClientError::StdIo(_) => Some("URL"), + _ => None, + } { + info!("known error: {e:?}"); + println!( + "{} probably wrong: {}", + style("Error!").red().bold(), + style(cause).cyan(), + ); + println!("{}", style(e.to_string()).yellow().italic()); + } else { + error!("unknown error: {e} ({e:?})"); + println!("{}", style("Unknown Error!").red().bold()); + } +} + fn main() { env_logger::init(); @@ -32,8 +54,8 @@ fn main() { let check_ctrlc = { let stop = Arc::new(AtomicBool::new(false)); - let stop_ctrlc = stop.clone(); + ctrlc::set_handler(move || { stop_ctrlc.store(true, Ordering::SeqCst); info!("stopping as soon as possible ..."); @@ -73,39 +95,20 @@ fn main() { state } Err(e) => { - if let Some(cause) = match e { - ClientError::ResponseStatus { - actual: 403, - expected: _, - } => Some("Alias ID"), - // ClientError::FileIO(_) => Some("URL"), - _ => None, - } { - info!("handling error: {e:?}"); - println!( - "{} probably wrong: {} – {:?}", - style("Error!").red().bold(), - style(cause).cyan(), - style(e.to_string()).yellow().italic() - ); - } else { - error!("unknown error: {e} – {e:?}"); - println!("{}", style("Unknown Error!").red().bold()); - } - + print_error(&e); exit(1); } } }); + info!("continuing with state: {state:?}"); + println!( "{} uploading: {}", style("ShrUpl").yellow().bold(), style(state.file_names().join(", ")).magenta(), ); - info!("continuing with state: {state:?}"); - loop { match state.upload_chunk(&agent) { Err(e) => error!("error: {e:?}"), diff --git a/src/sharry/client.rs b/src/sharry/client.rs index 7656770..5299b02 100644 --- a/src/sharry/client.rs +++ b/src/sharry/client.rs @@ -26,15 +26,18 @@ pub trait Client { #[derive(Debug, Error)] pub enum ClientError { + #[error(transparent)] + StdIo(#[from] std::io::Error), + #[error("network request failed: {0}")] Request(String), + #[error("unexpected response status: {actual} (expected {expected})")] + ResponseStatus { actual: u16, expected: u16 }, + #[error("response parsing failed: {0}")] ResponseParsing(String), - #[error("unexpected response status: {actual} (expected {expected:?})")] - ResponseStatus { actual: u16, expected: Option }, - #[error("unexpected response content: {0}")] ResponseContent(String), } @@ -57,7 +60,7 @@ impl ClientError { } else { Err(Self::ResponseStatus { actual: actual.into(), - expected: Some(expected.into()), + expected: expected.into(), }) } } @@ -66,11 +69,12 @@ impl ClientError { impl From for ClientError { fn from(value: ureq::Error) -> Self { match value { - ureq::Error::StatusCode(status) => ClientError::ResponseStatus { + ureq::Error::StatusCode(status) => Self::ResponseStatus { actual: status, - expected: None, + expected: 200, }, - error => Self::Request(error.to_string()), + ureq::Error::Io(e) => e.into(), + error => Self::req_err(error), } } }