Skip to main content

ServerlessError

Derive Macro ServerlessError 

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

Derive macro for error types that implement IntoErrorCode.

§Example

use server_less::ServerlessError;

#[derive(ServerlessError)]
enum MyError {
    #[error(code = NotFound, message = "User not found")]
    UserNotFound,
    #[error(code = 400)]  // HTTP status also works
    InvalidInput(String),
    // Code inferred from variant name
    Unauthorized,
}

This generates:

  • impl IntoErrorCode for MyError
  • impl Display for MyError
  • impl Error for MyError

§Attributes

  • #[error(code = X)] - Set error code (ErrorCode variant or HTTP status)
  • #[error(message = "...")] - Set custom message

Without attributes, the error code is inferred from the variant name.