Skip to content

Releases: dtolnay/thiserror

1.0.10

28 Jan 19:57
1.0.10
ccbb2ab
Compare
Choose a tag to compare
  • Improve parsing of .0 and .var-style format arguments (#54)

    For example the one here as the argument to the match expression would now be recognized correctly:

    #[derive(Error, Debug)]
    pub enum MyError {
        #[error("{}: {0}", match .1 {
            Some(n) => format!("a variant error occurred with n={}", n),
            None => format!("there was an empty variant error"),
        })]
        Variant(String, Option<usize>),
    }

1.0.9

04 Dec 08:02
1.0.9
Compare
Choose a tag to compare
  • Fix bug affecting display attributes of the form #[error("{}", some_expression)] (where the format string is "{}" and nothing else, and the value formatted is not just one of the fields from the error) #53

1.0.8

03 Dec 18:28
1.0.8
d53be52
Compare
Choose a tag to compare
  • Documentation improvements

1.0.7

03 Dec 18:27
1.0.7
79b740e
Compare
Choose a tag to compare
  • Support mixing shorthand and non-shorthand format args (#47)

    #[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 },
    }
  • Add #[error(transparent)] attribute for delegating Error impl to one field (#50)

    This is useful for hiding error variants from a library's public error type:

    #[derive(Error, Debug)]
    #[error(transparent)]  // source and Display delegate to ErrorKind
    pub struct Error(ErrorKind);
    
    #[derive(Error, Debug)]
    /*private*/ enum ErrorKind {
        #[error("...")]
        E0,
        #[error("...")]
        E1(#[source] io::Error),
    }

    And also for enums that need an "anything else" variant; such variants tend not to have their own Display message but just forward through to the underlying error's Display and source:

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

1.0.6

19 Nov 07:23
1.0.6
938bcec
Compare
Choose a tag to compare
  • Support Box<dyn Error + Send> as a #[source] (#41, thanks @mathstuf)

1.0.5

10 Nov 02:46
1.0.5
10b8e58
Compare
Choose a tag to compare
  • Support interpolating Path and PathBuf fields as if they had a Display impl

    use std::io;
    use std::path::PathBuf;
    use thiserror::Error;
    
    #[derive(Error, Debug)]
    pub enum Error {
        #[error("failed to load {1}")]
        Read(#[source] io::Error, PathBuf),
    }

    In previous releases this would fail to compile with:

    error[E0277]: `std::path::PathBuf` doesn't implement `std::fmt::Display`
     --> src/main.rs:7:13
      |
    7 |     #[error("failed to load {1}")]
      |             ^^^^^^^^^^^^^^^^^^^^ `std::path::PathBuf` cannot be formatted with the default formatter

1.0.4

27 Oct 15:20
1.0.4
5079141
Compare
Choose a tag to compare
  • Avoid generated variable name collision with an enum member named formatter (#36, thanks @mathstuf)

1.0.3

19 Oct 13:09
1.0.3
b2b3bae
Compare
Choose a tag to compare
  • Fix #[error("{v0}")] where an interpolated identifier contains number digits (#34)

1.0.2

13 Oct 01:29
1.0.2
ee864e1
Compare
Choose a tag to compare
  • Add a #[from] attribute to request an implementation of std::convert::From from your error's source error types, making it easy to build your error via the ? operator.

    use thiserror::Error;
    
    #[derive(Error, Debug)]
    pub enum Error {
        Io(#[from] io::Error),
        Json(#[from] serde_json::Error),
        Regex(#[from] regex::Error),
        Other(#[from] anyhow::Error),
    }

    We only permit From to be derived from the error's source field, not any arbitrary other field. Notice that this allows #[from] to imply #[source] so you don't need to also specify #[source] explicitly.

    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,
        },
    }
  • Named fields with the name source are assumed to be the error source and so no longer require an explicit #[source] attribute.

  • Enum variants now inherit the #[error(...)] attribute from atop the enum if there is one.

    use thiserror::Error;
    
    #[derive(Error, Debug)]
    #[error("{0}")] // applies to every variant without its own attr
    pub enum Error {
        Io(io::Error),
        Json(serde_json::Error),
        Regex(regex::Error),
        #[error("unknown error")]
        Unknown,
    }

1.0.1

09 Oct 20:08
1.0.1
87c80e7
Compare
Choose a tag to compare
  • Documentation improvements