From 9c306d55ce7369737de9dea9434260e86e12d0a7 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Wed, 21 Jul 2021 19:06:49 -0500 Subject: [PATCH] more specific errors for some IO errors --- src/config.rs | 18 +++++++++++++++--- src/editor.rs | 17 ++++++++++------- src/error.rs | 40 +++++++++++++++++++++++++++++++++++++++- src/formatters/csv.rs | 6 ++---- src/old.rs | 8 ++++---- 5 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/config.rs b/src/config.rs index a31ec47..fbd97f6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -97,7 +97,12 @@ impl Config { if config_filename.is_file() { Self::read_from_toml(config_filename) } else { - create_dir_all(project_dirs.config_dir())?; + let config_dir = project_dirs.config_dir(); + + create_dir_all(config_dir).map_err(|e| CouldntCreateConfigDir { + path: config_dir.to_owned(), + error: e.to_string(), + })?; Self::create_and_return_config(project_dirs.config_dir(), &config_filename) } } else { @@ -160,8 +165,15 @@ impl Config { ..Default::default() }; - let mut config_file = File::create(config_filename)?; - config_file.write_all(to_string(&config).unwrap().as_bytes())?; + let mut config_file = File::create(config_filename).map_err(|e| CouldntEditConfigFile { + path: config_filename.to_owned(), + error: e.to_string(), + })?; + + config_file.write_all(to_string(&config).unwrap().as_bytes()).map_err(|e| CouldntEditConfigFile { + path: config_filename.to_owned(), + error: e.to_string(), + })?; Ok(config) } diff --git a/src/editor.rs b/src/editor.rs index c0b10d8..233e63b 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -3,19 +3,19 @@ use std::io::Read; use tempfile::NamedTempFile; -use crate::error::{Error, Result}; +use crate::error::{Error::*, Result}; pub fn get_string(note_editor: Option<&String>) -> Result { let note_editor = if let Some(note_editor) = note_editor { note_editor } else { - return Err(Error::EditorIsEmpty); + return Err(EditorIsEmpty); }; let parts: Vec<_> = note_editor.split(" ").filter(|p| p.len() > 0).collect(); let editor = if let Some(name) = parts.get(0) { name.to_owned() } else { - return Err(Error::EditorIsEmpty); + return Err(EditorIsEmpty); }; let mut c = Command::new(editor); @@ -26,7 +26,7 @@ pub fn get_string(note_editor: Option<&String>) -> Result { } } - let mut tmpfile = NamedTempFile::new()?; + let mut tmpfile = NamedTempFile::new().map_err(|e| NoTmpFile(e.to_string()))?; c.arg(tmpfile.as_ref()); @@ -34,16 +34,19 @@ pub fn get_string(note_editor: Option<&String>) -> Result { .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) - .output()? + .output().map_err(|e| EditorFailed { + editor: note_editor.clone(), + error: e.to_string(), + })? .status; if status.success() { let mut note = String::new(); - tmpfile.read_to_string(&mut note)?; + tmpfile.read_to_string(&mut note).map_err(|e| CouldntReadTmpFile(e.to_string()))?; Ok(note) } else { - Err(Error::EditorFailed { editor: note_editor.clone(), error: status.to_string() }) + Err(EditorFailed { editor: note_editor.clone(), error: status.to_string() }) } } diff --git a/src/error.rs b/src/error.rs index a322f3a..5158368 100644 --- a/src/error.rs +++ b/src/error.rs @@ -108,7 +108,45 @@ t config --help for more options.")] EditorFailed { editor: String, error: String, - } + }, + + #[error("A temporary file couldn't be created for you to edit. This is very +weird, but might have something to do with yout OS. + +Here's the underlaying error: +{0} + +If you think this is an issue with this program report it at: + +https://gitlab.com/categulario/tiempo")] + NoTmpFile(String), + + #[error("The temporary file you just created couldn't be read. This is the +underlaying error: + +{0} + +I didn't imagine this could ever happen, but now I'm curious. Do you mind +reporging the issue at https://gitlab.com/categulario/tiempo ?")] + CouldntReadTmpFile(String), + + #[error("The ")] + CouldntCreateConfigDir { + path: PathBuf, + error: String, + }, + + #[error("A problem ocurred while trying to edit/create the config file at + +{path} + +Is there a permissions issue? Here's the underlaying error: + +{error}")] + CouldntEditConfigFile { + path: PathBuf, + error: String, + }, } pub type Result = result::Result; diff --git a/src/formatters/csv.rs b/src/formatters/csv.rs index 216aec8..3997987 100644 --- a/src/formatters/csv.rs +++ b/src/formatters/csv.rs @@ -3,7 +3,7 @@ use std::io::Write; use csv::Writer; use chrono::SecondsFormat; -use crate::error::Result; +use crate::error::{Result, Error::*}; use crate::models::Entry; pub fn print_formatted(entries: Vec, out: &mut W, ids: bool) -> Result<()> { @@ -34,9 +34,7 @@ pub fn print_formatted(entries: Vec, out: &mut W, ids: bool) -> } } - wtr.flush()?; - - Ok(()) + wtr.flush().map_err(|e| IOError(e)) } #[cfg(test)] diff --git a/src/old.rs b/src/old.rs index 7450229..8562712 100644 --- a/src/old.rs +++ b/src/old.rs @@ -3,7 +3,7 @@ use std::io::Write; use chrono::{DateTime, Utc, Local, LocalResult, TimeZone}; use ansi_term::Color::Yellow; -use crate::error::{Error, Result}; +use crate::error::{Error::*, Result}; use crate::models::Entry; use crate::database::{Database, DBVersion}; @@ -13,9 +13,9 @@ use crate::database::{Database, DBVersion}; /// Used to convert times from the old database version. fn local_to_utc(t: DateTime) -> Result> { let local_time = match Local.from_local_datetime(&t.naive_utc()) { - LocalResult::None => return Err(Error::NoneLocalTime(t.naive_utc().to_string())), + LocalResult::None => return Err(NoneLocalTime(t.naive_utc().to_string())), LocalResult::Single(t) => t, - LocalResult::Ambiguous(t1, t2) => return Err(Error::AmbiguousLocalTime { + LocalResult::Ambiguous(t1, t2) => return Err(AmbiguousLocalTime { orig: t.naive_utc().to_string(), t1: t1.naive_local(), t2: t2.naive_local(), @@ -77,7 +77,7 @@ pub fn warn_if_needed(err: &mut E, needs_warning: bool) -> Result<()> "{} You are using the old timetrap format, it is advised that \ you update your database using t migrate", Yellow.bold().paint("[WARNING]"), - )?; + ).map_err(|e| IOError(e))?; } Ok(())