Error messages with thiserror / anyhow

I'm using thiserror and anyhow to handle errors and when I call the ensure! macro from anyhow I get some additional message that's printed after the one I've implemented. Here are some code snippets:

errors.rs

use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Invalid tolerance. {} is not between 0.0 and 1.0", t)]
    ToleranceError { t: f32 }
}

main.rs

fn main() -> Result<(), anyhow::Error> {
    let input = user_input::args();
    ensure!(input.tolerance < 1.0 && input.tolerance > 0.0, errors::Error::ToleranceError { t: input.tolerance });
    // do stuff here
    Ok(())
}

When the errors is triggered, it prints:

Error: Invalid tolerance. 2 is not between 0.0 and 1.0
error: process didn't exit successfully: `....` (exit code: 1)

I don't want it to print the second line. How do I prevent this?

I believe that error is from cargo, not your app. Does it still appear if you run the application binary directly?

1 Like

Hehe, you're right. I didn't realize that. :smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.