Add graceful error handling to df script
This commit is contained in:
91
manager/src/error.rs
Normal file
91
manager/src/error.rs
Normal file
@ -0,0 +1,91 @@
|
||||
use compound_error::CompoundError;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Errors {
|
||||
errors: Vec<Error>,
|
||||
}
|
||||
|
||||
pub struct Error {
|
||||
location: PathBuf,
|
||||
inner: InnerError,
|
||||
}
|
||||
|
||||
#[derive(CompoundError, Debug)]
|
||||
pub enum InnerError {
|
||||
IoErr(io::Error),
|
||||
TemplateErr(templar::TemplarError),
|
||||
}
|
||||
|
||||
impl From<Vec<Error>> for Errors {
|
||||
fn from(errors: Vec<Error>) -> Self {
|
||||
Errors { errors }
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> From<E> for Errors
|
||||
where
|
||||
E: Into<Error>,
|
||||
{
|
||||
fn from(error: E) -> Self {
|
||||
Errors {
|
||||
errors: vec![error.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Errors {
|
||||
pub fn join(&mut self, mut other: Errors) {
|
||||
self.errors.append(&mut other.errors);
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.errors.is_empty()
|
||||
}
|
||||
|
||||
pub fn log(self) {
|
||||
if self.errors.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
error!("{} errors occured:", self.errors.len());
|
||||
for (i, error) in self.errors.iter().enumerate() {
|
||||
error!("{:.2}. {:?}", i, error.location);
|
||||
error!(" {:?}", error.inner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ErrorLocation {
|
||||
type Err;
|
||||
fn with_location(self, path: &Path) -> Self::Err;
|
||||
}
|
||||
|
||||
impl<T> ErrorLocation for T
|
||||
where
|
||||
T: Into<InnerError>,
|
||||
{
|
||||
type Err = Error;
|
||||
|
||||
fn with_location(self, path: &Path) -> Error {
|
||||
Error {
|
||||
location: path.to_owned(),
|
||||
inner: self.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> ErrorLocation for Result<T, E>
|
||||
where
|
||||
E: Into<InnerError>,
|
||||
{
|
||||
type Err = Result<T, Error>;
|
||||
|
||||
fn with_location(self, path: &Path) -> Result<T, Error> {
|
||||
self.map_err(|e| Error {
|
||||
location: path.to_owned(),
|
||||
inner: e.into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user