|
272 | 272 | //! ```
|
273 | 273 | //!
|
274 | 274 | //! `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 |
276 | 277 | //! the `Result` is an `Err` then `chain_err` evaluates the closure,
|
277 | 278 | //! which returns *some type that can be converted to `ErrorKind`*,
|
278 | 279 | //! boxes the original error to store as the cause, then returns a new
|
279 | 280 | //! error containing the original error.
|
280 | 281 | //!
|
| 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 | +//! |
281 | 322 | //! ## Matching errors
|
282 | 323 | //!
|
283 | 324 | //! error-chain error variants are matched with simple patterns.
|
|
346 | 387 | //! `From` conversions for regular links *do not introduce a new error
|
347 | 388 | //! into the error chain*, while conversions for foreign links *always
|
348 | 389 | //! 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 |
353 | 394 | //! are converted between each other to create a new `Error` but the
|
354 | 395 | //! old error is discarded; there is no "cause" created from the
|
355 | 396 | //! original error.
|
|
0 commit comments