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/main.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/main.rs')
-rw-r--r-- | src/main.rs | 67 |
1 files changed, 22 insertions, 45 deletions
diff --git a/src/main.rs b/src/main.rs index 8b6cb9a..06fb4ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,63 +29,40 @@ fn main() { if arg_matches.subcommand_matches("post").is_some() { log::info!("New post..."); - if let Err(e) = posts::create(&db) { - log::error!("Error creating new post"); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } + error::helper(posts::create(&db), "Error creating new post"); } else if arg_matches.subcommand_matches("update").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { - match val.value_of("id").unwrap_or_else(|| "0").parse() { - Ok(n) => n, - Err(e) => { - log::error!("Couldn't parse ID"); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } - } + error::helper( + val.value_of("id").unwrap_or_else(|| "0").parse(), + "Couldn't parse ID", + ) } else { 0 }; + log::info!("Updating post ..."); - if let Err(e) = posts::update_handler(&db, id) { - log::error!("Error updating post {}", id); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } + + error::helper( + posts::update_handler(&db, id), + format!("Error updating post {}", id).as_ref(), + ); } else if arg_matches.subcommand_matches("delete").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { - match val.value_of("id").unwrap_or_else(|| "0").parse() { - Ok(n) => n, - Err(_) => { - log::error!("Couldn't parse ID"); - std::process::exit(1); - } - } + error::helper( + val.value_of("id").unwrap_or_else(|| "0").parse(), + "Couldn't parse ID", + ) } else { 0 }; + log::info!("Deleting post"); - if let Err(e) = posts::delete_handler(&db, id) { - log::error!("Error deleting post {}", id); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } - } - if let Err(e) = posts::display(&db) { - log::error!("Error displaying posts"); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); + error::helper( + posts::delete_handler(&db, id), + format!("Error deleting post {}", id).as_ref(), + ); } + + error::helper(posts::display(&db), "Error displaying posts"); } |