chainerror/booksrc/tutorial11.md

34 lines
829 B
Markdown
Raw Normal View History

2018-12-20 16:37:08 +01:00
# Debug for the ErrorKind
2018-12-20 15:14:21 +01:00
2018-12-21 13:50:08 +01:00
One small improvement at the end of the tutorial is to fix the debug output of
`Func1ErrorKind`. As you probably noticed, the output doesn't say much of the enum.
~~~
Debug Error:
src/main.rs:35: Func2
[…]
~~~
As a lazy shortcut, we implement `Debug` by calling `Display` and end up with
~~~
Debug Error:
src/main.rs:40: func1 error calling func2
[…}
~~~
which gives us a lot more detail.
2018-12-20 15:14:21 +01:00
2018-12-21 16:11:58 +01:00
To create your own Errors, you might find [crates](https://crates.io) which create enum `Display+Debug` via derive macros.
Also noteworthy is [custom_error](https://crates.io/crates/custom_error) to define your custom errors,
which can then be used with `chainerror`.
2018-12-20 14:52:06 +01:00
~~~rust
2019-03-04 11:38:35 +01:00
{{#include ../examples/tutorial11.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
# }
2018-12-21 16:11:58 +01:00
~~~