I use thiserror crate for error handling inside my project.
I declare an error like this
#[derive(Debug, thiserror::Error)]
enum CustomErrors {
#[error("This is custom error one")]
CustomErrorOne,
#[error("This is custom error two")]
CustomErrorTwo
}
I use this custom error like this
// cut
match foo() {
Err(errors) -> match errors {
CustomErrors::CustomErrorOne => ..., // I want to get access to "This is custom error one" error message here
CustomErrors::CustomErrorTwo => ..., // ...and here
}
}
//cut
Am I understanding correctly that this is not possible due to the philosophy of the thiserror
? And it requires creating a new error message?
Thiserror deliberately does not appear in your public API. (c) Documentation
From the docs:
A
Display
impl is generated for your error if you provide#[error("...")]
messages on the struct or each variant of your enum...
So if you want to get that string from the CustomErrors
, you just need to call .to_string()
.