#error #error-handling #derive

nightly thiserror-ext

Useful extension utilities for thiserror

13 releases

0.1.1 Mar 6, 2024
0.1.0 Jan 15, 2024
0.0.11 Dec 7, 2023
0.0.9 Nov 29, 2023

#492 in Rust patterns

Download history 5327/week @ 2023-12-22 4324/week @ 2023-12-29 5816/week @ 2024-01-05 5548/week @ 2024-01-12 6445/week @ 2024-01-19 6260/week @ 2024-01-26 6042/week @ 2024-02-02 1027/week @ 2024-02-09 2599/week @ 2024-02-16 5904/week @ 2024-02-23 4559/week @ 2024-03-01 4190/week @ 2024-03-08 4997/week @ 2024-03-15 4905/week @ 2024-03-22 4158/week @ 2024-03-29 3532/week @ 2024-04-05

18,417 downloads per month

Apache-2.0

25KB
418 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>());
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");

Dependencies

~0.3–0.8MB
~19K SLoC