Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ pub enum DataStoreError {
}
```

Note that `#[from]` on the outer opaque type only supplies `From<ErrorRepr>`.
The `?` operator performs a single conversion step, so code returning
`PublicError` that uses `?` on one of the underlying error types directly
needs its own forwarding implementation on `PublicError`:

```rust
impl From<Foo> for PublicError {
fn from(err: Foo) -> Self {
Self(ErrorRepr::from(err))
}
}
```

- See also the [`anyhow`] library for a convenient single error type to use in
application code.

Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@
//! }
//! ```
//!
//! Note that `#[from]` on the outer opaque type only supplies
//! `From<ErrorRepr>`. The `?` operator performs a single conversion
//! step, so code returning `PublicError` that uses `?` on one of the
//! underlying error types directly needs its own forwarding
//! implementation on `PublicError`:
//!
//! ```
//! # use thiserror::Error;
//! #
//! # #[derive(Error, Debug)]
//! # #[error(transparent)]
//! # pub struct PublicError(#[from] ErrorRepr);
//! #
//! # #[derive(Error, Debug)]
//! # #[error("foo")]
//! # struct Foo;
//! #
//! # #[derive(Error, Debug)]
//! # enum ErrorRepr {
//! # #[error("foo")]
//! # Foo(#[from] Foo),
//! # }
//! #
//! impl From<Foo> for PublicError {
//! fn from(err: Foo) -> Self {
//! Self(ErrorRepr::from(err))
//! }
//! }
//! ```
//!
//! - See also the [`anyhow`] library for a convenient single error type to use
//! in application code.
//!
Expand Down