Commit 220c8c4a authored by Alexis Janon's avatar Alexis Janon
Browse files

ErrorKind::List list of errors (perror-like)

Was done manually for a lot of errors before:
```
err: Error;
return Err(Error::from(format!("Prepended context message: {}",
				err.to_string())));
```
Now:
```
return err.prepend("Prepended context message");
```
It appears much cleaner, and is easier to understand to: we are
_prepending_ an error message to the one we received.
parent 4297385a
......@@ -16,6 +16,7 @@ pub enum ErrorKind {
Json(serde_json::Error),
Simple(String),
Placeholder(()),
List { cur: Error, next: Error },
}
impl From<io::Error> for Error {
......@@ -56,6 +57,7 @@ impl StdError for Error {
// TODO: add cause to String errors?
ErrorKind::Simple(_) => None,
ErrorKind::Placeholder(_) => None,
ErrorKind::List { cur: _, ref next } => Some(next),
}
}
}
......@@ -67,6 +69,16 @@ impl fmt::Display for Error {
ErrorKind::Json(ref err) => err.fmt(f),
ErrorKind::Simple(ref err) => err.fmt(f),
ErrorKind::Placeholder(_) => write!(f, "placeholder"),
ErrorKind::List { ref cur, ref next } => write!(f, "{}: {}", cur, next),
}
}
}
impl Error {
pub fn prepend(self, error: impl Into<Error>) -> Self {
Error(Box::new(ErrorKind::List {
cur: error.into(),
next: self,
}))
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment