diff options
author | Ben Morrison <ben@gbmor.dev> | 2020-05-26 23:15:51 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2020-05-26 23:54:14 -0400 |
commit | 841e6a34a49261d2681c8a4d2c1dc176ba4e3b35 (patch) | |
tree | 726d6f77657ea79740a32c0ae55db88242591172 /src/error.rs | |
parent | 8ecc2943a4241d466a9bf5a359260a958e3d3b46 (diff) | |
download | clinte-841e6a34a49261d2681c8a4d2c1dc176ba4e3b35.tar.gz |
removed panics and refactored error handling
Using a helper function to handle fatal errors error::helper() Displays the simplified message if an error condition occurs. Displays both the simplified and the raw error message if -v verbose logging is enabled.
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/error.rs b/src/error.rs index 517b5fc..aa21021 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,15 +1,20 @@ +use crate::conf; + // This Result is used elsewhere, not in helper() pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; -pub fn helper<T, V>(res: std::result::Result<T, V>) -> T +pub fn helper<T, V>(res: std::result::Result<T, V>, simplified_message: &str) -> T where V: std::fmt::Debug, { match res { Ok(val) => val, Err(err) => { - log::error!("{:?}", err); - panic!("{:?}", err); + log::error!("{}", simplified_message); + if *conf::DEBUG { + log::error!("--> {:?}", err); + } + std::process::exit(1); } } } @@ -21,14 +26,7 @@ mod tests { #[test] fn shouldnt_panic() { let ok: std::result::Result<&str, &str> = Ok("okay"); - let rhs = helper(ok); + let rhs = helper(ok, "okay"); assert_eq!("okay", rhs); } - - #[test] - #[should_panic] - fn should_panic() { - let err: std::result::Result<&str, &str> = Err("oops"); - helper(err); - } } |