mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 00:56:41 +01:00
26 lines
896 B
Markdown
26 lines
896 B
Markdown
# ErrorKind to the rescue
|
|
|
|
To cope with different kind of errors, we introduce the kind of an error `Func1ErrorKind` with an enum.
|
|
|
|
Because we derive `Debug` and implement `Display` our `Func1ErrorKind` enum, this enum can be used as
|
|
a `std::error::Error`.
|
|
|
|
Not using `String` errors anymore, the `cherr!()` macro seen in the beginning of
|
|
the tutorial has to be used again.
|
|
|
|
Only returning `Func1ErrorKind` in `func1()` now let us get rid of `Result<(), Box<Error>>` and we can
|
|
use `ChainResult<(), Func1ErrorKind>`.
|
|
|
|
In `main` we can now directly use the methods of `ChainError<T>` without downcasting the error first.
|
|
|
|
Also a nice `match` on `ChainError<T>.kind()` is now possible, which returns `&T`, meaning
|
|
`&Func1ErrorKind` here.
|
|
|
|
~~~rust
|
|
use crate::chainerror::*;
|
|
{{#include ../examples/tutorial10.rs:2:}}
|
|
# #[allow(dead_code)]
|
|
# mod chainerror {
|
|
{{#includecomment ../src/lib.rs}}
|
|
# }
|
|
~~~ |