mirror of
https://github.com/haraldh/chainerror.git
synced 2025-05-31 13:58:07 +02:00
rename cherr()
to context()
This commit is contained in:
parent
2af5fb7ad6
commit
ed710fada3
25 changed files with 169 additions and 199 deletions
|
@ -9,7 +9,7 @@ this only
|
|||
prints out the last `Error`.
|
||||
|
||||
~~~
|
||||
Error: StringError("func1 error")
|
||||
Error: "func1 error"
|
||||
~~~
|
||||
|
||||
The next chapters of this tutorial show how `chainerror` adds more information
|
||||
|
|
|
@ -5,8 +5,8 @@ To cope with different kind of errors, we introduce the kind of an error `Func1E
|
|||
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.
|
||||
Not using `String` errors anymore, the `context()` function seen in the beginning of
|
||||
the tutorial can be used again.
|
||||
|
||||
Only returning `Func1ErrorKind` in `func1()` now let us get rid of `Result<(), Box<Error + Send + Sync>>` and we can
|
||||
use `ChainResult<(), Func1ErrorKind>`.
|
||||
|
@ -22,4 +22,4 @@ Also a nice `match` on `ChainError<T>.kind()` is now possible, which returns `&T
|
|||
# mod chainerror {
|
||||
{{#rustdoc_include ../src/lib.rs:-1}}
|
||||
# }
|
||||
~~~
|
||||
~~~
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Simple Chained String Errors
|
||||
|
||||
With relatively small changes and the help of the `cherr!` macro of the `chainerror` crate
|
||||
the `String` errors are now chained together.
|
||||
With relatively small changes and the help of the `context()` method of the `chainerror` crate
|
||||
the `&str` errors are now chained together.
|
||||
|
||||
Press the play button in the upper right corner and see the nice debug output.
|
||||
|
||||
|
@ -19,14 +19,13 @@ Press the play button in the upper right corner and see the nice debug output.
|
|||
{{#include ../examples/tutorial2.rs:13:15}}
|
||||
~~~
|
||||
|
||||
The macro `cherr!(olderror, newerror)` stores `olderror` as the source/cause of `newerror`
|
||||
along with the filename (`file!()`) and line number (`line!()`)
|
||||
and returns `newerror`.
|
||||
The function `context(newerror)` stores `olderror` as the source/cause of `newerror`
|
||||
along with the `Location` of the `context()` call and returns `Err(newerror)`.
|
||||
|
||||
`Err()?` then returns the inner error applying `.into()`, so that we
|
||||
`?` then returns the inner error applying `.into()`, so that we
|
||||
again have a `Err(Box<Error + Send + Sync>)` as a result.
|
||||
|
||||
The `Debug` implementation of `ChainError<T>` (which is returned by `cherr!()`)
|
||||
The `Debug` implementation of `ChainError<T>` (which is returned by `context()`)
|
||||
prints the `Debug` of `T` prefixed with the stored filename and line number.
|
||||
|
||||
`ChainError<T>` in our case is `ChainError<String>`.
|
||||
`ChainError<T>` in our case is `ChainError<&str>`.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Mapping Errors
|
||||
|
||||
Now let's get more rust idiomatic by using `.map_err()`.
|
||||
Now let's get more rust idiomatic by using `.context()` directly on the previous `Result`.
|
||||
|
||||
~~~rust
|
||||
{{#include ../examples/tutorial3.rs}}
|
||||
|
@ -26,4 +26,4 @@ src/main.rs:16: "func1 error"
|
|||
This is, because we caught the error of `func1()` in `main()` and print it out ourselves.
|
||||
|
||||
We can now control, whether to output in `Debug` or `Display` mode.
|
||||
Maybe depending on `--debug` as a CLI argument.
|
||||
Maybe depending on `--debug` as a CLI argument.
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
# Saving coding chars
|
||||
# More information
|
||||
|
||||
Because decorating an error with more information should not
|
||||
let you jump through hoops, `chainerror` has a quick macro for that.
|
||||
|
||||
`mstrerror!()` fits right into `.map_err()` letting you quickly add
|
||||
more debug strings.
|
||||
|
||||
`mstrerror!()` even understands `format!()` syntax like `println!()`.
|
||||
To give more context to the error, you want to use `format!`
|
||||
to extend the information in the context string.
|
||||
|
||||
~~~rust
|
||||
{{#include ../examples/tutorial4.rs}}
|
||||
|
@ -14,4 +9,4 @@ more debug strings.
|
|||
# mod chainerror {
|
||||
{{#rustdoc_include ../src/lib.rs:-1}}
|
||||
# }
|
||||
~~~
|
||||
~~~
|
||||
|
|
|
@ -4,8 +4,8 @@ To distinguish the errors occuring in various places, we can define named string
|
|||
"new type" pattern.
|
||||
|
||||
~~~rust,ignore
|
||||
derive_str_cherr!(Func2Error);
|
||||
derive_str_cherr!(Func1Error);
|
||||
derive_str_context!(Func2Error);
|
||||
derive_str_context!(Func1Error);
|
||||
~~~
|
||||
|
||||
Instead of `ChainError<String>` we now have `struct Func1Error(String)` and `ChainError<Func1Error>`.
|
||||
|
@ -28,4 +28,4 @@ hiding the `ChainError<T>` implementation detail.
|
|||
# mod chainerror {
|
||||
{{#rustdoc_include ../src/lib.rs:-1}}
|
||||
# }
|
||||
~~~
|
||||
~~~
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue