Skip to main content

AuthError

Derive Macro AuthError 

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

Implements From<E> for AuthError and ErrorCodes for an enum of plugin errors.

use fastauth_core::plugin_prelude::*;

#[derive(AuthError, Debug)]
#[auth_error(prefix = "API_KEY")]
pub enum ApiKeyError {
    #[auth_error(status = 404, message = "API key not found")]
    NotFound,
    #[auth_error(status = 403, message = "scope {scope} is not allowed")]
    ScopeDenied { scope: String },
}

let err = AuthError::from(ApiKeyError::ScopeDenied { scope: "admin".into() });
assert_eq!(err.code(), "API_KEY_SCOPE_DENIED");
assert_eq!(err.message(), "scope admin is not allowed");

Each variant’s code is the prefix and the variant name in SCREAMING_SNAKE_CASE (API_KEY_NOT_FOUND); without a prefix it is the variant name alone. Messages can name fields of struct-like variants in {field} placeholders, formatted with Display.