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/ed.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/ed.rs')
-rw-r--r-- | src/ed.rs | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/src/ed.rs b/src/ed.rs index 31a27ae..095fd9a 100644 --- a/src/ed.rs +++ b/src/ed.rs @@ -4,22 +4,15 @@ use std::process; use chrono::prelude::*; -use crate::conf; use crate::error; use crate::user; -fn create_tmp_file<'a>() -> Result<String, &'a str> { +fn create_tmp_file<'a>() -> Result<String, std::io::Error> { let the_time = Utc::now().to_rfc3339(); let file_name = format!("/tmp/clinte_ed_{}_{}", *user::NAME, the_time); match fs::write(&file_name, "") { Ok(_) => Ok(file_name), - Err(err) => { - log::warn!("Couldn't create tempfile"); - if *conf::DEBUG { - log::warn!("--> {:?}", err); - } - Err("Unable to create temp file") - } + Err(err) => Err(err), } } @@ -40,7 +33,7 @@ pub fn call() -> String { } }; - let tmp_loc = error::helper(create_tmp_file()); + let tmp_loc = error::helper(create_tmp_file(), "Couldn't create tempfile"); error::helper( process::Command::new(editor) @@ -48,9 +41,13 @@ pub fn call() -> String { .stdin(process::Stdio::inherit()) .stdout(process::Stdio::inherit()) .output(), + "Couldn't call editor", ); - let body = error::helper(fs::read_to_string(&tmp_loc)); - error::helper(fs::remove_file(tmp_loc)); + let body = error::helper( + fs::read_to_string(&tmp_loc), + "Couldn't read message from disk", + ); + error::helper(fs::remove_file(tmp_loc), "Couldn't remove temporary file"); body } |