20 releases
| 0.4.0-alpha | Mar 6, 2026 |
|---|---|
| 0.3.1-alpha.2 | Dec 4, 2025 |
| 0.3.1-alpha.1 | Jun 18, 2025 |
| 0.2.1 | Nov 20, 2024 |
| 0.0.9 | Nov 29, 2023 |
#212 in Rust patterns
19,571 downloads per month
Used in 18 crates
(10 directly)
33KB
568 lines
thiserror-ext
Useful extension utilities for thiserror. See the documentation for more details.
#[derive(
Debug,
thiserror::Error,
thiserror_ext::Box,
thiserror_ext::Construct,
thiserror_ext::ContextInto,
thiserror_ext::Macro,
)]
#[thiserror_ext(
newtype(name = Error, backtrace),
macro(path = "crate::foo"),
)]
enum ErrorKind {
#[error("cannot parse int from `{from}`")]
Parse {
source: std::num::ParseIntError,
from: String,
},
#[error("not yet implemented: {msg}")]
NotImplemented {
issue: Option<i32>,
#[message] msg: String,
},
#[error("internal error: {0}")]
Internal(String),
}
// `thiserror_ext::Construct`
let error: Error = Error::internal("oops");
// `thiserror_ext::Box`
assert_eq!(std::mem::size_of::<Error>(), std::mem::size_of::<usize>());
// You can access the backtrace through `backtrace()` method, or nightly-only `std::error::request_ref`.
let bt: &std::backtrace::Backtrace = error.backtrace().unwrap();
let bt: &Backtrace = std::error::request_ref(&error).unwrap();
// `thiserror_ext::ContextInto`
let result: Result<i32, Error> = "foo".parse().into_parse("foo");
// `thiserror_ext::AsReport`
//
// "cannot parse int from `foo`: invalid digit found in string"
println!("{}", result.unwrap_err().as_report());
// `thiserror_ext::Macro`
bail_not_implemented!(issue = 42, "an {} feature", "awesome");
Features
-
std(default): enablesstdintegration. In particular,#[thiserror_ext(newtype(.., backtrace))]captures backtraces and the generated newtype exposes.backtrace(). Disable default features forno_std+alloc:thiserror-ext = { version = "...", default-features = false } -
nightly: enable nightly features ofError, especiallystd::error::Error::provide. This enables:-
forwarding provided members from generated newtypes;
-
backtrace de-duplication when source errors already provide one;
-
custom provide logic through
extra_provide:#[derive(thiserror::Error, thiserror_ext::Arc)] #[thiserror_ext(newtype(name = Error, extra_provide = Self::my_extra_provide))] enum ErrorInner { #[error("...")] Foo, } impl Error { fn my_extra_provide(&self, request: &mut std::error::Request<'_>) { // request.provide_value(...); // request.provide_ref(...); } }
-
Dependencies
~150–540KB
~13K SLoC