macro_rules! define_errors {
(
$(
$code:expr => [
$( $name:ident ),+ $(,)?
]
),+ $(,)?
) => { ... };
}Expand description
Batch defines multiple typed errors under the same error code.
This macro allows you to define multiple error types that share the same error code,
making it more convenient to organize related errors. Each error type automatically
implements From for Error, allowing seamless conversion.
§Syntax
define_errors! {
ErrorCode::SomeCode => [
ErrorType1,
ErrorType2,
ErrorType3,
],
ErrorCode::AnotherCode => [
ErrorType4,
],
}§Examples
use lxy::{define_errors, error::{ErrorCode, Result, Error}};
define_errors! {
ErrorCode::ResourceNotFound => [
UserNotFound,
ProjectNotFound,
FileNotFound,
],
ErrorCode::InvalidInput => [
InvalidEmail,
InvalidPassword,
],
}§Using with Direct Conversion
fn get_user(id: u32) -> Result<String> {
if id == 0 {
return Err(UserNotFound.into());
}
Ok("User found".to_string())
}§Using with Custom Messages
use lxy::{define_errors, error::{ErrorCode, Result, TypedError}};
define_errors! {
ErrorCode::ResourceNotFound => [UserNotFound],
}
fn get_user(id: u32) -> Result<String> {
if id == 0 {
return Err(UserNotFound::error(format!("User {} not found", id)));
}
Ok("User found".to_string())
}