Document all the display representations
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Tolnay · committed by David Tolnay · parent 7da2a83 · Browse files · 1 file·+85 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+85 added−0 removed
- other:+85 added−0 removed
Provenance
- Commit
42d0286- Tree
b649f61- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
7da2a83
1 file changed+85 added−0 removed
modified:src/lib.rs+85 added
193 unmodified lines194 /// - `Error` guarantees that a backtrace is available, even if the underlying195 /// error type does not provide one.196 /// - `Error` is represented as a narrow pointer — exactly one word in197 /// size instead of two.198+///199+/// <br>200+///201+/// # Display representations202+///203+/// When you print an error object using "{}" or to_string(), only the outermost204+/// underlying error or context is printed, not any of the lower level causes.205+/// This is exactly as if you had called the Display impl of the error from206+/// which you constructed your anyhow::Error.207+///208+/// ```console209+/// failed to read instrs from ./path/to/instrs.jsox210+/// ```211+///212+/// To print causes as well using anyhow's default formatting of causes, use the213+/// alternate selector "{:#}".214+///215+/// ```console216+/// failed to read instrs from ./path/to/instrs.jsox217+///218+/// Caused by:219+/// No such file or directory (os error 2)220+/// ```221+///222+/// The Debug format "{:?}" includes your backtrace if one was captured. Note223+/// that this is the representation you get by default if you return an error224+/// from `fn main` instead of printing it explicitly yourself.225+///226+/// ```console227+/// Error: failed to read instrs from ./path/to/instrs.jsox228+///229+/// Caused by:230+/// No such file or directory (os error 2)231+///232+/// Stack backtrace:233+/// 0: <E as anyhow::context::ext::StdError>::ext_context234+/// at /git/anyhow/src/backtrace.rs:26235+/// 1: core::result::Result<T,E>::map_err236+/// at /git/rustc/src/libcore/result.rs:596237+/// 2: anyhow::context::<impl anyhow::Context<T,E> for core::result::Result<T,E>>::with_context238+/// at /git/anyhow/src/context.rs:58239+/// 3: testing::main240+/// at src/main.rs:5241+/// 4: std::rt::lang_start242+/// at /git/rustc/src/libstd/rt.rs:61243+/// 5: main244+/// 6: __libc_start_main245+/// 7: _start246+/// ```247+///248+/// To see a conventional struct-style Debug representation, use "{:#?}".249+///250+/// ```console251+/// Error {252+/// context: "failed to read instrs from ./path/to/instrs.jsox",253+/// source: Os {254+/// code: 2,255+/// kind: NotFound,256+/// message: "No such file or directory",257+/// },258+/// }259+/// ```260+///261+/// If none of the built-in representations are appropriate and you would prefer262+/// to render the error and its cause chain yourself, it can be done something263+/// like this:264+///265+/// ```266+/// use anyhow::{Context, Result};267+///268+/// fn main() {269+/// if let Err(err) = try_main() {270+/// eprintln!("ERROR: {}", err);271+/// err.chain().skip(1).for_each(|cause| eprintln!("because: {}", cause));272+/// std::process::exit(1);273+/// }274+/// }275+///276+/// fn try_main() -> Result<()> {277+/// # const IGNORE: &str = stringify! {278+/// ...279+/// # };280+/// # Ok(())281+/// }282+/// ```283 pub struct Error {284 inner: ManuallyDrop<Box<ErrorImpl<()>>>,285 }286 248 unmodified lines