This library provides a convenient derive macro for the standard library's std::error::Error trait.

Related tags

Command-line therror
Overview

derive(Error)

github crates.io docs.rs build status

This library provides a convenient derive macro for the standard library's std::error::Error trait.

[dependencies]
therror = "1.0"

Compiler support: requires rustc 1.56+


Example

use therror::Error;

#[derive(Error, Debug)]
pub enum DataStoreError {
    #[error("data store disconnected")]
    Disconnect(#[from] io::Error),
    #[error("the data for key `{0}` is not available")]
    Redaction(String),
    #[error("invalid header (expected {expected:?}, found {found:?})")]
    InvalidHeader {
        expected: String,
        found: String,
    },
    #[error("unknown data store error")]
    Unknown,
}

Details

  • Thiserror deliberately does not appear in your public API. You get the same thing as if you had written an implementation of std::error::Error by hand, and switching from handwritten impls to therror or vice versa is not a breaking change.

  • Errors may be enums, structs with named fields, tuple structs, or unit structs.

  • A Display impl is generated for your error if you provide #[error("...")] messages on the struct or each variant of your enum, as shown above in the example.

    The messages support a shorthand for interpolating fields from the error.

    • #[error("{var}")] ⟶ write!("{}", self.var)
    • #[error("{0}")] ⟶ write!("{}", self.0)
    • #[error("{var:?}")] ⟶ write!("{:?}", self.var)
    • #[error("{0:?}")] ⟶ write!("{:?}", self.0)

    These shorthands can be used together with any additional format args, which may be arbitrary expressions. For example:

    #[derive(Error, Debug)]
    pub enum Error {
        #[error("invalid rdo_lookahead_frames {0} (expected < {})", i32::MAX)]
        InvalidLookahead(u32),
    }

    If one of the additional expression arguments needs to refer to a field of the struct or enum, then refer to named fields as .var and tuple fields as .0.

    #[derive(Error, Debug)]
    pub enum Error {
        #[error("first letter must be lowercase but was {:?}", first_char(.0))]
        WrongCase(String),
        #[error("invalid index {idx}, expected at least {} and at most {}", .limits.lo, .limits.hi)]
        OutOfBounds { idx: usize, limits: Limits },
    }
  • A From impl is generated for each variant containing a #[from] attribute.

    Note that the variant must not contain any other fields beyond the source error and possibly a backtrace. A backtrace is captured from within the From impl if there is a field for it.

    #[derive(Error, Debug)]
    pub enum MyError {
        Io {
            #[from]
            source: io::Error,
            backtrace: Backtrace,
        },
    }
  • The Error trait's source() method is implemented to return whichever field has a #[source] attribute or is named source, if any. This is for identifying the underlying lower level error that caused your error.

    The #[from] attribute always implies that the same field is #[source], so you don't ever need to specify both attributes.

    Any error type that implements std::error::Error or dereferences to dyn std::error::Error will work as a source.

    #[derive(Error, Debug)]
    pub struct MyError {
        msg: String,
        #[source]  // optional if field name is `source`
        source: anyhow::Error,
    }
  • The Error trait's provide() method is implemented to provide whichever field has a type named Backtrace, if any, as a std::backtrace::Backtrace.

    use std::backtrace::Backtrace;
    
    #[derive(Error, Debug)]
    pub struct MyError {
        msg: String,
        backtrace: Backtrace,  // automatically detected
    }
  • If a field is both a source (named source, or has #[source] or #[from] attribute) and is marked #[backtrace], then the Error trait's provide() method is forwarded to the source's provide so that both layers of the error share the same backtrace.

    #[derive(Error, Debug)]
    pub enum MyError {
        Io {
            #[backtrace]
            source: io::Error,
        },
    }
  • Errors may use error(transparent) to forward the source and Display methods straight through to an underlying error without adding an additional message. This would be appropriate for enums that need an "anything else" variant.

    #[derive(Error, Debug)]
    pub enum MyError {
        ...
    
        #[error(transparent)]
        Other(#[from] anyhow::Error),  // source and Display delegate to anyhow::Error
    }

    Another use case is hiding implementation details of an error representation behind an opaque error type, so that the representation is able to evolve without breaking the crate's public API.

    // PublicError is public, but opaque and easy to keep compatible.
    #[derive(Error, Debug)]
    #[error(transparent)]
    pub struct PublicError(#[from] ErrorRepr);
    
    impl PublicError {
        // Accessors for anything we do want to expose publicly.
    }
    
    // Private and free to change across minor version of the crate.
    #[derive(Error, Debug)]
    enum ErrorRepr {
        ...
    }
  • See also the anyhow library for a convenient single error type to use in application code.


Comparison to anyhow

Use therror if you care about designing your own dedicated error type(s) so that the caller receives exactly the information that you choose in the event of failure. This most often applies to library-like code. Use Anyhow if you don't care what error type your functions return, you just want it to be easy. This is common in application-like code.


A fork of therror

This fork was created with the idea of adding a killer-feature to therror which would definitely not make it into therror itself. We shall write more once it is more than an idea.

The idea is to start out at 0.X.Y where X is chosen to denote breaking changes, and Y is tracking the therror baseline while it matters. Once it becomes clear how therror differentiates itself, while retaining compatibility to therror for its baseline features, and once this differentiation becomes mature, we can continue increasing the major version to 2.0.0 and stand alone.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
You might also like...
🍬 shell-candy is a library that wraps Rust's `std::process::Command`

🍬 shell-candy is a library that wraps Rust's `std::process::Command`, providing a functional mechanism for handling stdout/stderr streams of spawned tasks..

Rust derive-based argument parsing optimized for code size

Argh Argh is an opinionated Derive-based argument parser optimized for code size Derive-based argument parsing optimized for code size and conformance

A simple trait-based framework for the annual Advent of Code programming challenge.

lib_aoc lib_aoc is a simple trait-based framework for the annual Advent of Code programming challenge. Focus less on the boilerplate and more on the p

Extension trait to chunk iterators into const-length arrays.

const-chunks This crate provides an extension trait that lets you chunk iterators into constant-length arrays using const generics. See the docs for m

Derive forms from structs.

leptos_form: Derive leptos forms from rust structs Documentation Docs GitHub repository Cargo package Minimum supported Rust version: 1.75.0 or later

Prototype of the `std::io::ensure!` family of macros

io-ensure Prototype of the `std::io::ensure` family of macros API Docs | Releases | Contributing Installation $ cargo add io-ensure Safety This crate

An alternative `std`-like implementation built on origin

origin-studio An alternative `std`-like implementation built on origin origin-stdio is an alternative std-like implementation built on origin. At this

A drop-in replacement for std::time::Instant that is faster and more accurate.

Fastant A drop-in replacement for std::time::Instant that measures time with high performance and high accuracy powered by Time Stamp Counter (TSC). U

Rust CLI utility library. Error handling, status reporting, and exit codes.
Rust CLI utility library. Error handling, status reporting, and exit codes.

narrate This library provides CLI application error and status reporting utilities. The coloured output formatting aims to be similar to Cargo. Error

Releases(v0.0.48)
  • v0.0.48(Sep 7, 2023)

    The initial release which really is just a renamed fork of thiserror. Thanks to dtolnay for providing such a good base for further development.

    Commit Statistics

    • 1 commit contributed to the release.
    • 4 days passed between releases.
    • 0 commits were understood as conventional.
    • 0 issues like '(#ID)' were seen in commit messages

    Commit Details

    view details
    • Uncategorized
      • Rename thiserror to theerror (b19ab24)
    Source code(tar.gz)
    Source code(zip)
Owner
Sebastian Thiel
Sebastian Thiel
Irx-config - The library provides convenient way to represent/parse configuration from different sources

The irx-config library provides convenient way to represent/parse configuration from different sources. The main goals is to be very easy to use and t

Andriy Bakay 2 Sep 14, 2022
A convenient macro for building PathBufs in Rust.

pathbuf pathbuf is a simple crate which provides the pathbuf macro to conveniently construct the Rust PathBuf type. Example use pathbuf::pathbuf; use

Andrew Lilley Brinker 6 Jan 16, 2023
`matchable` provides a convenient enum for checking if a piece of text is matching a string or a regex.

matchable matchable provides a convenient enum for checking if a piece of text is matching a string or a regex. The common usage of this crate is used

Pig Fang 6 Dec 19, 2022
A micro crate that simplifies a bit the use of the std macro thread_local!.

with-thread-local A micro crate that simplifies a bit the use of the std macro thread_local!. extern crate regex; use with_thread_local::with_thread_

Cecile Tonglet 3 Jan 11, 2023
Bam Error Stats Tool (best): analysis of error types in aligned reads.

best Bam Error Stats Tool (best): analysis of error types in aligned reads. best is used to assess the quality of reads after aligning them to a refer

Google 54 Jan 3, 2023
Simple but convenient CLI-based Matrix client app for sending and receiving (in Rust)

matrix-commander-rs simple but convenient CLI-based Matrix client app for sending and receiving Help create this Rust program! This project is current

null 19 Dec 30, 2022
simple but convenient CLI-based Nostr client app for following users and sending DMs

nostr-commander-rs TLDR: simple but convenient CLI-based Nostr client app for publishing, sending DMs, as well as following users and channels nostr-c

null 18 Dec 30, 2022
A simple yet convenient cross-platform ARP spoofer

ruuf - A simple yet convenient cross-platform ARP spoofer Poison the ARP cache of the given victim, thereby redirecting the traffic to the target ther

null 6 Feb 19, 2023
A more convenient `#[target_feature]` replacement

A more convenient #[target_feature] replacement To get good performance out of SIMD everything on the SIMD codepath must be inlined. With how SIMD is

Koute 3 Apr 10, 2023
argmax is a library that allows Rust applications to avoid Argument list too long errors (E2BIG) by providing a std::process::Command wrapper with a

argmax argmax is a library that allows Rust applications to avoid Argument list too long errors (E2BIG) by providing a std::process::Command wrapper w

David Peter 22 Nov 20, 2022