massa icon indicating copy to clipboard operation
massa copied to clipboard

Improve errors messages

Open aoudiamoncef opened this issue 2 years ago • 0 comments

Improve errors messages by adding the context of what caused the error.

For example, when I try to parse an argument: bad:

/// takes a slice of string and makes it into a `Vec<T>`
pub fn parse_vec<T: std::str::FromStr>(args: &[String]) -> anyhow::Result<Vec<T>, Error>
where
    T::Err: Display,
{
    args.iter()
        .map(|x| {
            x.parse::<T>()
                .map_err(|e| anyhow!("Parsing error: {}", e))
        })
        .collect()
}

If the example above, I don't know which argument in the array caused the parsing error.

Good:

/// takes a slice of string and makes it into a `Vec<T>`
pub fn parse_vec<T: std::str::FromStr>(args: &[String]) -> anyhow::Result<Vec<T>, Error>
where
    T::Err: Display,
{
    args.iter()
        .map(|x| {
            x.parse::<T>()
                .map_err(|e| anyhow!("failed to parse \"{}\" due to: {}", x, e))
        })
        .collect()
}

Here we know exactly which arguments is causing the error.

I saw an example in when I tried to start a node and I got: Failed to read NodeID but I didn't know which configuration is causing this, but if I got: Failed to parse NodeId "toto" is a better

aoudiamoncef avatar Jan 11 '23 10:01 aoudiamoncef