2018-12-20 16:37:08 +01:00
|
|
|
# Simple Chained String Errors
|
2018-12-20 15:14:21 +01:00
|
|
|
|
2020-09-01 21:03:01 +02:00
|
|
|
With relatively small changes and the help of the `context()` method of the `chainerror` crate
|
|
|
|
the `&str` errors are now chained together.
|
2018-12-20 15:14:21 +01:00
|
|
|
|
|
|
|
Press the play button in the upper right corner and see the nice debug output.
|
|
|
|
|
2018-12-20 14:52:06 +01:00
|
|
|
~~~rust
|
2019-03-04 11:38:35 +01:00
|
|
|
{{#include ../examples/tutorial2.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-20 15:14:21 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
### What did we do here?
|
|
|
|
|
|
|
|
~~~rust,ignore
|
2018-12-21 13:50:08 +01:00
|
|
|
{{#include ../examples/tutorial2.rs:13:15}}
|
2018-12-20 15:14:21 +01:00
|
|
|
~~~
|
|
|
|
|
2020-09-01 21:03:01 +02:00
|
|
|
The function `context(newerror)` stores `olderror` as the source/cause of `newerror`
|
|
|
|
along with the `Location` of the `context()` call and returns `Err(newerror)`.
|
2018-12-20 15:14:21 +01:00
|
|
|
|
2020-09-01 21:03:01 +02:00
|
|
|
`?` then returns the inner error applying `.into()`, so that we
|
2019-03-12 16:43:53 +01:00
|
|
|
again have a `Err(Box<Error + Send + Sync>)` as a result.
|
2018-12-20 15:14:21 +01:00
|
|
|
|
2023-07-28 16:21:22 +02:00
|
|
|
The `Debug` implementation of `chainerror::Error<T>` (which is returned by `context()`)
|
2018-12-20 15:14:21 +01:00
|
|
|
prints the `Debug` of `T` prefixed with the stored filename and line number.
|
|
|
|
|
2023-07-28 16:21:22 +02:00
|
|
|
`chainerror::Error<T>` in our case is `chainerror::Error<&str>`.
|