chainerror/booksrc/tutorial10.md

22 lines
785 B
Markdown
Raw Normal View History

2018-12-20 16:37:08 +01:00
# ErrorKind to the rescue
2018-12-20 15:14:21 +01:00
To cope with different kind of errors, we introduce the `kind` of an error `Func1ErrorKind` with an enum.
2018-12-21 13:50:08 +01:00
Because we derive `Debug` and implement `Display` our `Func1ErrorKind` enum, this enum can be used as
a `std::error::Error`.
2019-03-12 16:43:53 +01:00
Only returning `Func1ErrorKind` in `func1()` now let us get rid of `Result<(), Box<Error + Send + Sync>>` and we can
2018-12-21 13:50:08 +01:00
use `ChainResult<(), Func1ErrorKind>`.
In `main` we can now directly use the methods of `chainerror::Error<T>` without downcasting the error first.
2018-12-21 13:50:08 +01:00
Also, a nice `match` on `chainerror::Error<T>.kind()` is now possible, which returns `&T`, meaning `&Func1ErrorKind` here.
2018-12-20 15:14:21 +01:00
2018-12-20 14:52:06 +01:00
~~~rust
2019-03-04 11:38:35 +01:00
{{#include ../examples/tutorial10.rs}}
2018-12-20 14:52:06 +01:00
# #[allow(dead_code)]
# mod chainerror {
2020-03-03 14:25:37 +01:00
{{#rustdoc_include ../src/lib.rs:-1}}
2018-12-20 14:52:06 +01:00
# }
2020-09-01 21:03:01 +02:00
~~~