chainerror/booksrc/tutorial8.md

32 lines
851 B
Markdown
Raw Normal View History

2018-12-20 16:37:08 +01:00
# Finding an Error cause
2018-12-20 15:14:21 +01:00
To distinguish the errors occurring in various places, we can define named string errors with the
2018-12-21 13:50:08 +01:00
"new type" pattern.
~~~rust,ignore
chainerror::str_context!(Func2Error);
chainerror::str_context!(Func1Error);
2018-12-21 13:50:08 +01:00
~~~
Instead of `chainerror::Error<String>` we now have `struct Func1Error(String)` and `chainerror::Error<Func1Error>`.
2018-12-21 13:50:08 +01:00
In the `main` function you can see, how we can match the different errors.
Also see:
~~~rust,ignore
if let Some(f2err) = f1err.find_chain_cause::<Func2Error>() {
~~~
as a shortcut to
~~~rust,ignore
if let Some(f2err) = f1err.find_cause::<chainerror::Error<Func2Error>>() {
2018-12-21 13:50:08 +01:00
~~~
hiding the `chainerror::Error<T>` implementation detail.
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/tutorial8.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
~~~