Skip to content

How to combine errors? #424

Answered by shepmaster
bryanlarsen asked this question in Q&A
Discussion options

You must be logged in to vote

If I understand what you want, I'd probably end up with

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(display("Parse Error on {field}: {source}"))]
    ParseError {
        field: String,
        source: InnerParseError,
    },
}

#[derive(Debug, Snafu)]
pub enum InnerParseError {
    #[snafu(display("YAMLError {source}"))]
    YamlError { source: serde_yaml::Error },

    #[snafu(display("JSONError {source}"))]
    JsonError { source: serde_json::Error },

    #[snafu(display("{message}"))]
    MessageError { message: String },
}

fn main_wrapped() -> Result<String, Error> {
    let r: i32 = rand::thread_rng().gen_range(0..3);

    Ok(match r {
        0 => {
            let _ = s…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by bryanlarsen
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #323 on November 29, 2023 21:38.