|
383 | 383 | //! # }
|
384 | 384 | //! ```
|
385 | 385 | //!
|
| 386 | +//! ## Inspecting errors |
| 387 | +//! |
| 388 | +//! An error-chain error contains information about the error itself, a backtrace, and the chain |
| 389 | +//! of causing errors. For reporting purposes, this information can be accessed as follows. |
| 390 | +//! |
| 391 | +//! ``` |
| 392 | +//! # #[macro_use] extern crate error_chain; |
| 393 | +//! use error_chain::ChainedError; // for e.display() |
| 394 | +//! |
| 395 | +//! error_chain! { |
| 396 | +//! errors { |
| 397 | +//! InvalidToolchainName(t: String) { |
| 398 | +//! description("invalid toolchain name") |
| 399 | +//! display("invalid toolchain name: '{}'", t) |
| 400 | +//! } |
| 401 | +//! } |
| 402 | +//! } |
| 403 | +//! |
| 404 | +//! # fn main() { |
| 405 | +//! // Generate an example error to inspect: |
| 406 | +//! let e = "xyzzy".parse::<i32>() |
| 407 | +//! .chain_err(|| ErrorKind::InvalidToolchainName("xyzzy".to_string())) |
| 408 | +//! .unwrap_err(); |
| 409 | +//! |
| 410 | +//! // Get the brief description of the error: |
| 411 | +//! assert_eq!(e.description(), "invalid toolchain name"); |
| 412 | +//! |
| 413 | +//! // Get the display version of the error: |
| 414 | +//! assert_eq!(e.to_string(), "invalid toolchain name: 'xyzzy'"); |
| 415 | +//! |
| 416 | +//! // Get the full cause and backtrace: |
| 417 | +//! println!("{}", e.display().to_string()); |
| 418 | +//! // Error: invalid toolchain name: 'xyzzy' |
| 419 | +//! // Caused by: invalid digit found in string |
| 420 | +//! // stack backtrace: |
| 421 | +//! // 0: 0x7fa9f684fc94 - backtrace::backtrace::libunwind::trace |
| 422 | +//! // at src/backtrace/libunwind.rs:53 |
| 423 | +//! // - backtrace::backtrace::trace<closure> |
| 424 | +//! // at src/backtrace/mod.rs:42 |
| 425 | +//! // 1: 0x7fa9f6850b0e - backtrace::capture::{{impl}}::new |
| 426 | +//! // at out/capture.rs:79 |
| 427 | +//! // [..] |
| 428 | +//! # } |
| 429 | +//! ``` |
| 430 | +//! |
| 431 | +//! The `Error` and `ErrorKind` types also allow programmatic access to these elements. |
| 432 | +//! |
386 | 433 | //! ## Foreign links
|
387 | 434 | //!
|
388 | 435 | //! Errors that do not conform to the same conventions as this library
|
|
0 commit comments