Skip to main content

Status

Derive Macro Status 

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

Give an error type a response status with #[status(..)], or give each variant of an enum its own status. When ? converts an error of the type, the error responds with that status, as with Errors::status. A status set with Errors::status takes precedence.

use laravel::errors::Status;

#[derive(Debug, thiserror::Error, Status)]
#[error("This time slot is already booked")]
#[status(409)]
pub struct BookingConflict;

#[derive(Debug, thiserror::Error, Status)]
pub enum PaymentError {
    #[error("The card was declined: {reason}")]
    #[status(402)]
    Declined { reason: String },

    #[error("The amount {0} is below the minimum")]
    #[status(422)]
    BelowMinimum(u64),

    // No status, so a `500`...
    #[error("the payment provider failed")]
    Provider,
}

On an enum, a #[status(..)] on the type applies to the variants that don’t set their own. The status is a number or a StatusCode. The derive doesn’t support generic types.

A number that isn’t a valid status code fails to compile:

ⓘ
#[derive(Debug, laravel::errors::Status)]
#[status(42)]
struct NotAStatus;