diff --git a/README.md b/README.md index 7c67cb2..418785f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum DataStoreError { - #[error("data store disconnected")] + #[error("data store disconnected: {0}")] Disconnect(#[from] io::Error), #[error("the data for key `{0}` is not available")] Redaction(String), @@ -93,6 +93,10 @@ pub enum DataStoreError { source error (and possibly a backtrace — see below). Usually `#[from]` fields are unnamed, but `#[from]` is allowed on a named field too. + To include the source's `Display` output in your error message, use `{0}` + (or `{var}` for a named field) in the `#[error("...")]` format string, as in + the `Disconnect` example above. + ```rust #[derive(Error, Debug)] pub enum MyError { diff --git a/src/lib.rs b/src/lib.rs index 9e79f67..b76cbff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! //! #[derive(Error, Debug)] //! pub enum DataStoreError { -//! #[error("data store disconnected")] +//! #[error("data store disconnected: {0}")] //! Disconnect(#[from] io::Error), //! #[error("the data for key `{0}` is not available")] //! Redaction(String), @@ -104,6 +104,10 @@ //! `#[from]` fields are unnamed, but `#[from]` is allowed on a named field //! too. //! +//! To include the source's [`Display`] output in your error message, use +//! `{0}` (or `{var}` for a named field) in the `#[error("...")]` format +//! string, as in the `Disconnect` example above. +//! //! ```rust //! # use core::fmt::{self, Display}; //! # use std::io;