Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add top-level documentation on reporting errors #466

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//! - [`Options`](OptionExt)
#![cfg_attr(feature = "futures", doc = " - [`Futures`](futures::TryFutureExt)")]
#![cfg_attr(feature = "futures", doc = " - [`Streams`](futures::TryStreamExt)")]
//! - [Error reporting](#reporting)
//! - Suitable for libraries and applications
//! - `no_std` compatibility
//! - Generic types and lifetimes
Expand Down Expand Up @@ -200,6 +201,63 @@
//! your error type to be used in multithreaded programs, by changing
//! `dyn std::error::Error` to `dyn std::error::Error + Send + Sync`.
//!
//! ## Reporting
//!
//! Printing an error via [`Display`][]
//! will only show the top-level error message without the underlying sources.
//! For an extended error report,
//! SNAFU offers a user-friendly error output mechanism.
//! It prints the main error and all underlying errors in the chain,
//! from the most recent to the oldest,
//! plus the [backtrace](Backtrace) if applicable.
//! This is done by using the [`macro@report`] procedural macro
//! or the [`Report`] type directly.
//!
//! ```no_run
//! use snafu::prelude::*;
//!
//! #[derive(Debug, Snafu)]
//! #[snafu(display("Could not load configuration file {path}"))]
//! struct ConfigFileError {
//! source: std::io::Error,
//! path: String,
//! }
//!
//! fn read_config_file(path: &str) -> Result<String, ConfigFileError> {
//! std::fs::read_to_string(path).context(ConfigFileSnafu { path })
//! }
//!
//! #[snafu::report]
//! fn main() -> Result<(), ConfigFileError> {
//! read_config_file("bad-config.ini")?;
//! Ok(())
//! }
//! ```
//!
//! This will print:
//!
//! ```none
//! Error: Could not load configuration file bad-config.ini
//!
//! Caused by this error:
//! 1: No such file or directory (os error 2)
//! ```
//!
//! Which shows the underlying errors, unlike [`Display`]:
//!
//! ```none
//! Error: Could not load configuration file bad-config.ini
//! ```
//!
//! ... and is also more readable than the [`Debug`] output:
//!
//! ```none
//! Error: ConfigFileError { source: Os { code: 2, kind: NotFound, message: "No such file or directory" }, path: "bad-config.ini" }
//! ```
//!
//! [`Display`]: core::fmt::Display
//! [`Debug`]: core::fmt::Debug
//!
//! ## Next steps
//!
//! Read the documentation for the [`Snafu`][] macro to see all of the
Expand Down