Derive Macro AsApiError

Source
#[derive(AsApiError)]
{
    // Attributes available to this derive:
    #[error]
}
Expand description

This derive macro is used to convert an enum into an ApiError.
You can use it by adding the #[derive(AsApiError)] attribute to your enum.
By default, the kind is snake case.
#[error(kind = "your_message_id")] attribute to the variant.
You can also add a custom code to the error by adding the #[error(code = 400)] attribute to the variant.
The following status are available and return the corresponding status code:

fn get_status_code(error_kind: &str) -> u16 {
    match error_kind {
        "BadRequest" => 400,
        "Unauthorized" => 401,
        "Forbidden" => 403,
        "NotFound" => 404,
        "MethodNotAllowed" => 405,
        "Conflict" => 409,
        "Gone" => 410,
        "PayloadTooLarge" => 413,
        "UnsupportedMediaType" => 415,
        "UnprocessableEntity" => 422,
        "TooManyRequests" => 429,
        "InternalServerError" => 500,
        "NotImplemented" => 501,
        "BadGateway" => 502,
        "ServiceUnavailable" => 503,
        "GatewayTimeout" => 504,
        _ => 0, // Or some other default/error handling
    }
}

// Example usage:
let code = get_status_code("NotFound");
assert_eq!(code, 404);
let default_code = get_status_code("SomeOtherError");
assert_eq!(default_code, 0);