Skip to content
Myr

Copy readme to crate level doc

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 97314ac · Browse files · 1 file+76 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+76 added−0 removed

  • other:+76 added−0 removed

1 file+76 added−0 removed

modified:src/lib.rs+76 added
1//! This library provides [`anyhow::Error`][Error], a trait object based error2//! type for easy idiomatic error handling in Rust applications.3//!4//! <br>5//!6//! # Details7//!8//! - Use `Result<T, anyhow::Error>`, or equivalently `anyhow::Result<T>`, as9//! the return type of any fallible function.10//!11//! Within the function, use `?` to easily propagate any error that implements12//! the `std::error::Error` trait.13//!14//! ```15//! use anyhow::Result;16//!17//! fn get_cluster_info() -> Result<ClusterMap> {18//! let config = std::fs::read_to_string("cluster.json")?;19//! let map: ClusterMap = serde_json::from_str(&config)?;20//! Ok(map)21//! }22//! ```23//!24//! - Attach context to help the person troubleshooting the error understand25//! where things went wrong. A low-level error like "No such file or26//! directory" can be annoying to debug without more context about what higher27//! level step the application was in the middle of.28//!29//! ```30//! use anyhow::{Context, Result};31//!32//! fn main() -> Result<()> {33//! ...34//! it.detach().context("failed to detach the important thing")?;35//!36//! let content = std::fs::read(path)37//! .with_context(|| format!("failed to read instrs from {}", path))?;38//! ...39//! }40//! ```41//!42//! ```console43//! Error: failed to read instrs from ./path/to/instrs.jsox44//!45//! caused by:46//! 0: No such file or directory (os error 2)47//! ```48//!49//! - Downcasting is supported and can be by value, by shared reference, or by50//! mutable reference as needed.51//!52//! ```53//! // If the error was caused by redaction, then return a54//! // tombstone instead of the content.55//! match root_cause.downcast_ref::<DataStoreError>() {56//! Some(DataStoreError::Censored(_)) => Ok(Async::Ready(REDACTED_CONTENT)),57//! None => Err(e),58//! }59//! ```60//!61//! - A backtrace is captured and printed with the error if the underlying error62//! type does not already provide its own. In order to see backtraces, the63//! `RUST_LIB_BACKTRACE=1` environment variable must be defined.64//!65//! <br>66//!67//! # Acknowledgements68//!69//! The implementation of the `anyhow::Error` type is identical to70//! `fehler::Exception` ([https://github.com/withoutboats/fehler][fehler]). This71//! library just exposes it under the more standard `Error` / `Result`72//! terminology rather than the `throw!` / `#[throws]` / `Exception` language of73//! exceptions.74//!75//! [fehler]: https://github.com/withoutboats/fehler7677#![cfg_attr(backtrace, feature(backtrace))]7879mod context;80mod error;
57 unmodified lines