Expand description
§match_err
Macro for quick matching and asserting errors against enum-like error types
Helps to avoid writing long and tedious structures like:
if let Err(e) = err {
if let Some(e) = e.downcast_ref::<Error>() {
match e {
...
}
}
}
§Examples
use match_err::*;
use anyhow::anyhow;
#[derive(thiserror::Error, Debug)]
enum Error {
#[error("not found")]
NotFound,
#[error("custom: {0}")]
Custom(String),
}
let err: Result<(), _> = Err(anyhow!(Error::NotFound));
match_if_err!(err, Error, {
NotFound => println!("not found"),
Custom(msg) => println!("custom message: {}", msg),
_ => println!("unknown")
})
Macros§
- assert_
error - Asserts the error against an enum-like error type by hiding the usage of downcast_ref method The error is required to implement PartialEq
- assert_
if_ error - Asserts the variable is an error and then asserts it against an enum-like error type by hiding the usage of downcast_ref method The error is required to implement PartialEq
- match_
err - Matches the error against an enum-like error type by hiding the usage of downcast_ref method
- match_
if_ err - Checks if it’s an error and matches the error against an enum-like error type by hiding the usage of downcast_ref method