[wip] impl Client for ureq::Agent
- error handling refactoring
This commit is contained in:
parent
9b1f7f872c
commit
67da081ef9
2 changed files with 37 additions and 30 deletions
49
src/main.rs
49
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:?}"),
|
||||
|
|
|
|||
|
|
@ -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<u16> },
|
||||
|
||||
#[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<ureq::Error> 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue