[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 cli::Cli;
|
||||||
use sharry::ClientError;
|
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() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
|
|
@ -32,8 +54,8 @@ fn main() {
|
||||||
|
|
||||||
let check_ctrlc = {
|
let check_ctrlc = {
|
||||||
let stop = Arc::new(AtomicBool::new(false));
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
let stop_ctrlc = stop.clone();
|
let stop_ctrlc = stop.clone();
|
||||||
|
|
||||||
ctrlc::set_handler(move || {
|
ctrlc::set_handler(move || {
|
||||||
stop_ctrlc.store(true, Ordering::SeqCst);
|
stop_ctrlc.store(true, Ordering::SeqCst);
|
||||||
info!("stopping as soon as possible ...");
|
info!("stopping as soon as possible ...");
|
||||||
|
|
@ -73,39 +95,20 @@ fn main() {
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let Some(cause) = match e {
|
print_error(&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());
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
info!("continuing with state: {state:?}");
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{} uploading: {}",
|
"{} uploading: {}",
|
||||||
style("ShrUpl").yellow().bold(),
|
style("ShrUpl").yellow().bold(),
|
||||||
style(state.file_names().join(", ")).magenta(),
|
style(state.file_names().join(", ")).magenta(),
|
||||||
);
|
);
|
||||||
|
|
||||||
info!("continuing with state: {state:?}");
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match state.upload_chunk(&agent) {
|
match state.upload_chunk(&agent) {
|
||||||
Err(e) => error!("error: {e:?}"),
|
Err(e) => error!("error: {e:?}"),
|
||||||
|
|
|
||||||
|
|
@ -26,15 +26,18 @@ pub trait Client {
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum ClientError {
|
pub enum ClientError {
|
||||||
|
#[error(transparent)]
|
||||||
|
StdIo(#[from] std::io::Error),
|
||||||
|
|
||||||
#[error("network request failed: {0}")]
|
#[error("network request failed: {0}")]
|
||||||
Request(String),
|
Request(String),
|
||||||
|
|
||||||
|
#[error("unexpected response status: {actual} (expected {expected})")]
|
||||||
|
ResponseStatus { actual: u16, expected: u16 },
|
||||||
|
|
||||||
#[error("response parsing failed: {0}")]
|
#[error("response parsing failed: {0}")]
|
||||||
ResponseParsing(String),
|
ResponseParsing(String),
|
||||||
|
|
||||||
#[error("unexpected response status: {actual} (expected {expected:?})")]
|
|
||||||
ResponseStatus { actual: u16, expected: Option<u16> },
|
|
||||||
|
|
||||||
#[error("unexpected response content: {0}")]
|
#[error("unexpected response content: {0}")]
|
||||||
ResponseContent(String),
|
ResponseContent(String),
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +60,7 @@ impl ClientError {
|
||||||
} else {
|
} else {
|
||||||
Err(Self::ResponseStatus {
|
Err(Self::ResponseStatus {
|
||||||
actual: actual.into(),
|
actual: actual.into(),
|
||||||
expected: Some(expected.into()),
|
expected: expected.into(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,11 +69,12 @@ impl ClientError {
|
||||||
impl From<ureq::Error> for ClientError {
|
impl From<ureq::Error> for ClientError {
|
||||||
fn from(value: ureq::Error) -> Self {
|
fn from(value: ureq::Error) -> Self {
|
||||||
match value {
|
match value {
|
||||||
ureq::Error::StatusCode(status) => ClientError::ResponseStatus {
|
ureq::Error::StatusCode(status) => Self::ResponseStatus {
|
||||||
actual: status,
|
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