Macro error_ex::create_error
source · macro_rules! create_error { ( $error:ident => $( $error_reason:ident ),* ) => { ... }; }
Expand description
Defining an error with set of reason.
Usage
use error_ex::{create_error};
create_error!(ErrorType => ErrorReason1, ErrorReason2, ErrorReason3);
Examples
use error_ex::{create_error};
create_error!(InputError => IllegalArgument, InvalidInput, MissingArgument);
//Now, you can use the following code to instantiate this error
InputError::illegal_argument(format!("Your message here"));
Error Mapping
The explicit way
use std::fs;
use error_ex::{create_error};
create_error!(FileError => IoError);
create_error!(SchemaError => ParseError);
let error: Result<(), FileError> = Err(FileError::io_error("".to_string()));
let mapped = error.map_err(|error| {
SchemaError::parse_error(format!("SchemaError::ParseError error {error}"))
});
Function wrapper
The above code can be simplified using map_to__error!
macro using the build in lower order function
use std::fs;
use std::io::Error;
use error_ex::{create_error};
create_error!(FileError => IoError);
create_error!(SchemaError => ParseError);
let error: Result<(), FileError> = Err(FileError::io_error("".to_string()));
let mapped = error.map_err(SchemaError::map_to_parse_error);