769a5a68e2
Phase 5: Replace all Box<dyn Error> return types with anyhow::Result<T>
throughout the codebase. Replace string-based Err("msg".into()) and
format!().into() patterns with bail!() and anyhow!() macros. Fix
dirs::home_dir().unwrap() in settings.rs to use a fallback path instead
of panicking when HOME is unset. Remove stray use std::error::Error
imports no longer needed.
Phase 6: Add From<&User> for CacheUser in models/user.rs and
From<&Laboratory>/From<&Laboratories> for CacheLaboratory/CacheLabsWrapper
in models/laboratory.rs. Simplify commands/login.rs to use .into()
conversions, removing the redundant to_cache_user() and to_cache_labs()
helper functions.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
26 lines
848 B
Rust
26 lines
848 B
Rust
/// Print the error message and exit with code 2.
|
|
/// JSON deserialization errors produce a friendlier message matching Python's
|
|
/// JSONDecodeError handling.
|
|
pub fn handle_error(e: anyhow::Error) -> ! {
|
|
if is_json_error(&*e) {
|
|
eprintln!(
|
|
"Unexpected response returned. Please check the configuration or the server's operational status."
|
|
);
|
|
} else {
|
|
eprintln!("Error: {}", e);
|
|
}
|
|
std::process::exit(2);
|
|
}
|
|
|
|
/// Walk the error source chain to detect `serde_json` parse errors.
|
|
fn is_json_error(e: &(dyn std::error::Error + 'static)) -> bool {
|
|
let mut source: Option<&(dyn std::error::Error + 'static)> = Some(e);
|
|
while let Some(err) = source {
|
|
if err.downcast_ref::<serde_json::Error>().is_some() {
|
|
return true;
|
|
}
|
|
source = err.source();
|
|
}
|
|
false
|
|
}
|