Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit b8f4e06

Browse files
kw217Yamakaky
authored andcommitted
Improve docs on linked and chained errors. (#145)
* Clarify usage for chaining and linking. * Add section on linked errors, and fix references. * Document with_chain as introduced by #126.
1 parent 7effe4a commit b8f4e06

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

examples/quickstart.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ mod errors {
1717
error_chain! { }
1818
}
1919

20+
// This only gives access within this module. Make this `pub use errors::*;`
21+
// instead if the types must be accessible from other modules (e.g., within
22+
// a `links` section).
2023
use errors::*;
2124

2225
fn main() {

src/lib.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,53 @@
272272
//! ```
273273
//!
274274
//! `chain_err` can be called on any `Result` type where the contained
275-
//! error type implements `std::error::Error + Send + 'static`. If
275+
//! error type implements `std::error::Error + Send + 'static`, as long as
276+
//! the `Result` type's corresponding `ResultExt` trait is in scope. If
276277
//! the `Result` is an `Err` then `chain_err` evaluates the closure,
277278
//! which returns *some type that can be converted to `ErrorKind`*,
278279
//! boxes the original error to store as the cause, then returns a new
279280
//! error containing the original error.
280281
//!
282+
//! To chain an error directly, use `with_chain`:
283+
//!
284+
//! ```
285+
//! # #[macro_use] extern crate error_chain;
286+
//! # fn main() {}
287+
//! # error_chain! {}
288+
//! # fn do_something() -> Result<()> { unimplemented!() }
289+
//! # fn test() -> Result<()> {
290+
//! let res: Result<()> =
291+
//! do_something().map_err(|e| Error::with_chain(e, "something went wrong"));
292+
//! # Ok(())
293+
//! # }
294+
//! ```
295+
//!
296+
//! ## Linking errors
297+
//!
298+
//! To convert an error from another error chain to this error chain:
299+
//!
300+
//! ```
301+
//! # #[macro_use] extern crate error_chain;
302+
//! # fn main() {}
303+
//! # mod other { error_chain! {} }
304+
//! error_chain! {
305+
//! links {
306+
//! OtherError(other::Error, other::ErrorKind);
307+
//! }
308+
//! }
309+
//!
310+
//! fn do_other_thing() -> other::Result<()> { unimplemented!() }
311+
//!
312+
//! # fn test() -> Result<()> {
313+
//! let res: Result<()> = do_other_thing().map_err(|e| e.into());
314+
//! # Ok(())
315+
//! # }
316+
//! ```
317+
//!
318+
//! The `Error` and `ErrorKind` types implements `From` for the corresponding
319+
//! types of all linked error chains. Linked errors do not introduce a new
320+
//! cause to the error chain.
321+
//!
281322
//! ## Matching errors
282323
//!
283324
//! error-chain error variants are matched with simple patterns.
@@ -346,10 +387,10 @@
346387
//! `From` conversions for regular links *do not introduce a new error
347388
//! into the error chain*, while conversions for foreign links *always
348389
//! introduce a new error into the error chain*. So for the example
349-
//! above all errors deriving from the `temp::Error` type will be
350-
//! presented to the user as a new `ErrorKind::Temp` variant, and the
351-
//! cause will be the original `temp::Error` error. In contrast, when
352-
//! `rustup_utils::Error` is converted to `Error` the two `ErrorKind`s
390+
//! above all errors deriving from the `std::fmt::Error` type will be
391+
//! presented to the user as a new `ErrorKind::Fmt` variant, and the
392+
//! cause will be the original `std::fmt::Error` error. In contrast, when
393+
//! `other_error::Error` is converted to `Error` the two `ErrorKind`s
353394
//! are converted between each other to create a new `Error` but the
354395
//! old error is discarded; there is no "cause" created from the
355396
//! original error.

0 commit comments

Comments
 (0)