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

Commit c723ff7

Browse files
kw217Yamakaky
authored andcommitted
Add "Inspecting errors" section to top-level docs. (#167)
* Add "Inspecting errors" section to top-level docs. * Make example a bit more realistic. * Add a comment to the example for added clarity.
1 parent e2bdf55 commit c723ff7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,53 @@
383383
//! # }
384384
//! ```
385385
//!
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+
//!
386433
//! ## Foreign links
387434
//!
388435
//! Errors that do not conform to the same conventions as this library

0 commit comments

Comments
 (0)