rustthiserror

How to use ? to convert to a newtype error variant of thiserror::Error


I can not figure out how to avoid needing to use 'map_err' to first map the external crate's error to my crate's enum Error.

At the moment I have this solution with a newtype, but I don't like having to call map_err everywhere. How can I fix this?

A playground with my current workaround, and a little comment on what I would like to write instead: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=15e5ee935fe801f64cbb082722f0775b.


Solution

  • I was making this way too difficult. Here's the simple solution: Implement From for Error:

    impl From<ExternalError> for Error {
        fn from(e: ExternalError) -> Self {
            Error::Other(OtherError::new(e))
        }
    }