#error-handling #derive #error

no-std thiserror-ext

Useful extension utilities for thiserror

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

Download history 19962/week @ 2025-12-04 19701/week @ 2025-12-11 13759/week @ 2025-12-18 4972/week @ 2025-12-25 8686/week @ 2026-01-01 2675/week @ 2026-01-08 3135/week @ 2026-01-15 3173/week @ 2026-01-22 3092/week @ 2026-01-29 3237/week @ 2026-02-05 3383/week @ 2026-02-12 2564/week @ 2026-02-19 4561/week @ 2026-02-26 8142/week @ 2026-03-05 3195/week @ 2026-03-12 3113/week @ 2026-03-19

19,571 downloads per month
Used in 18 crates (10 directly)

Apache-2.0

33KB
568 lines

thiserror-ext

Crate Docs

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): enables std integration. In particular, #[thiserror_ext(newtype(.., backtrace))] captures backtraces and the generated newtype exposes .backtrace(). Disable default features for no_std + alloc:

    thiserror-ext = { version = "...", default-features = false }
    
  • nightly: enable nightly features of Error, especially std::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