mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 09:04:14 +01:00
impl Deref for ChainError
This commit is contained in:
parent
ef7edb1061
commit
4762a75cfe
12
booksrc/tutorial12.md
Normal file
12
booksrc/tutorial12.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# Deref for the ErrorKind
|
||||||
|
|
||||||
|
Because ChainError<T> implements Deref to &T, we can also match on `*e` instead of `e.kind()`.
|
||||||
|
|
||||||
|
~~~rust
|
||||||
|
use crate::chainerror::*;
|
||||||
|
{{#include ../examples/tutorial12.rs:2:}}
|
||||||
|
# #[allow(dead_code)]
|
||||||
|
# mod chainerror {
|
||||||
|
{{#includecomment ../src/lib.rs}}
|
||||||
|
# }
|
||||||
|
~~~
|
64
examples/tutorial12.rs
Normal file
64
examples/tutorial12.rs
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
use chainerror::*;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::io;
|
||||||
|
use std::result::Result;
|
||||||
|
|
||||||
|
fn do_some_io() -> Result<(), Box<Error>> {
|
||||||
|
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
derive_str_cherr!(Func2Error);
|
||||||
|
|
||||||
|
fn func2() -> Result<(), Box<Error>> {
|
||||||
|
let filename = "foo.txt";
|
||||||
|
do_some_io().map_err(mstrerr!(Func2Error, "Error reading '{}'", filename))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Func1ErrorKind {
|
||||||
|
Func2,
|
||||||
|
IO(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::std::fmt::Display for Func1ErrorKind {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Func1ErrorKind::Func2 => write!(f, "func1 error calling func2"),
|
||||||
|
Func1ErrorKind::IO(filename) => write!(f, "Error reading '{}'", filename),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::std::fmt::Debug for Func1ErrorKind {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
|
write!(f, "{}", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::std::error::Error for Func1ErrorKind {}
|
||||||
|
|
||||||
|
fn func1() -> ChainResult<(), Func1ErrorKind> {
|
||||||
|
func2().map_err(|e| cherr!(e, Func1ErrorKind::Func2))?;
|
||||||
|
let filename = String::from("bar.txt");
|
||||||
|
do_some_io().map_err(|e| cherr!(e, Func1ErrorKind::IO(filename)))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<Error>> {
|
||||||
|
if let Err(e) = func1() {
|
||||||
|
match *e {
|
||||||
|
Func1ErrorKind::Func2 => eprintln!("Main Error Report: func1 error calling func2"),
|
||||||
|
Func1ErrorKind::IO(ref filename) => {
|
||||||
|
eprintln!("Main Error Report: func1 error reading '{}'", filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(e) = e.find_chain_cause::<Func2Error>() {
|
||||||
|
eprintln!("\nError reported by Func2Error: {}", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
eprintln!("\nDebug Error:\n{:?}", e);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -374,6 +374,14 @@ impl<'a> Iterator for ErrorIter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: 'static + Display + Debug> std::ops::Deref for ChainError<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.kind()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience trait to hide the `ChainError<T>` implementation internals
|
/// Convenience trait to hide the `ChainError<T>` implementation internals
|
||||||
pub trait ChainErrorDown {
|
pub trait ChainErrorDown {
|
||||||
/// Test if of type `ChainError<T>`
|
/// Test if of type `ChainError<T>`
|
||||||
|
|
Loading…
Reference in a new issue