improve errors for IO

This commit is contained in:
Abraham Toriz 2021-07-23 20:01:43 -05:00
parent 88e6c21672
commit 325f53e6d4
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 27 additions and 8 deletions

View File

@ -117,9 +117,15 @@ impl Config {
let path: PathBuf = path.as_ref().into();
let mut contents = String::new();
let mut file = File::open(&path).map_err(|_| FileNotFound(path.clone()))?;
let mut file = File::open(&path).map_err(|e| CouldntReadConfigFile {
path: path.clone(),
error: e,
})?;
file.read_to_string(&mut contents).map_err(|_| CouldntRead(path.clone()))?;
file.read_to_string(&mut contents).map_err(|e| CouldntReadConfigFile {
path: path.clone(),
error: e,
})?;
serde_yaml::from_str(&contents).map_err(|error| YamlError {
path, error
@ -130,9 +136,15 @@ impl Config {
let path: PathBuf = path.as_ref().into();
let mut contents = String::new();
let mut file = File::open(&path).map_err(|_| FileNotFound(path.clone()))?;
let mut file = File::open(&path).map_err(|e| CouldntReadConfigFile {
path: path.clone(),
error: e,
})?;
file.read_to_string(&mut contents).map_err(|_| CouldntRead(path.clone()))?;
file.read_to_string(&mut contents).map_err(|e| CouldntReadConfigFile {
path: path.clone(),
error: e,
})?;
toml::from_str(&contents).map_err(|error| TomlError {
path, error

View File

@ -1,5 +1,6 @@
use std::result;
use std::path::PathBuf;
use std::io;
use thiserror::Error;
use chrono::NaiveDateTime;
@ -15,11 +16,17 @@ pub enum Error {
#[error("Could not find home directory or a place where configuration can be read an written :(")]
NoHomeDir,
#[error("The file {0} was not found")]
FileNotFound(PathBuf),
#[error("Could not read the config file at
#[error("Error reading the file: {0}")]
CouldntRead(PathBuf),
{path}
with error:
{error}")]
CouldntReadConfigFile {
path: PathBuf,
error: io::Error,
},
#[error("Couldn't parse toml file at: {path}\nwith error: {error}")]
TomlError{ path: PathBuf, error: toml::de::Error},