improve errors for IO
This commit is contained in:
parent
88e6c21672
commit
325f53e6d4
|
@ -117,9 +117,15 @@ impl Config {
|
||||||
let path: PathBuf = path.as_ref().into();
|
let path: PathBuf = path.as_ref().into();
|
||||||
|
|
||||||
let mut contents = String::new();
|
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 {
|
serde_yaml::from_str(&contents).map_err(|error| YamlError {
|
||||||
path, error
|
path, error
|
||||||
|
@ -130,9 +136,15 @@ impl Config {
|
||||||
let path: PathBuf = path.as_ref().into();
|
let path: PathBuf = path.as_ref().into();
|
||||||
|
|
||||||
let mut contents = String::new();
|
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 {
|
toml::from_str(&contents).map_err(|error| TomlError {
|
||||||
path, error
|
path, error
|
||||||
|
|
15
src/error.rs
15
src/error.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::result;
|
use std::result;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use chrono::NaiveDateTime;
|
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 :(")]
|
#[error("Could not find home directory or a place where configuration can be read an written :(")]
|
||||||
NoHomeDir,
|
NoHomeDir,
|
||||||
|
|
||||||
#[error("The file {0} was not found")]
|
#[error("Could not read the config file at
|
||||||
FileNotFound(PathBuf),
|
|
||||||
|
|
||||||
#[error("Error reading the file: {0}")]
|
{path}
|
||||||
CouldntRead(PathBuf),
|
|
||||||
|
with error:
|
||||||
|
|
||||||
|
{error}")]
|
||||||
|
CouldntReadConfigFile {
|
||||||
|
path: PathBuf,
|
||||||
|
error: io::Error,
|
||||||
|
},
|
||||||
|
|
||||||
#[error("Couldn't parse toml file at: {path}\nwith error: {error}")]
|
#[error("Couldn't parse toml file at: {path}\nwith error: {error}")]
|
||||||
TomlError{ path: PathBuf, error: toml::de::Error},
|
TomlError{ path: PathBuf, error: toml::de::Error},
|
||||||
|
|
Loading…
Reference in New Issue