fallback

Attribute Macro fallback 

#[fallback]
Expand description

Define a fallback handler.

A fallback handler is invoked when no other route matches the incoming request. It’s typically used to return a 404 Not Found response or redirect to a default page.

§Example

use pavex::{fallback, Response};

#[fallback]
pub async fn not_found() -> Response {
    Response::not_found()
        .set_typed_body("The page you are looking for does not exist")
}

§Guide

Check out the “Routing” section of Pavex’s guide for a thorough introduction to routing in Pavex applications.

§Registration

Use Blueprint::fallback to register the fallback handler with your Blueprint.

The #[fallback] macro generates a constant that you can use to refer to the fallback handler when invoking Blueprint::fallback.

§Arguments

The sections below provide an exhaustive list of all the arguments and flags supported by the fallback macro:

NameKindRequired
idArgumentNo

§id

By default, Pavex generates a constant named after your function (converted to UPPER_SNAKE_CASE) that you use when registering the fallback handler.

The id argument allows you to customize the name of the generated constant.

§Example

Using the default generated identifier:

use pavex::{fallback, Blueprint, Response};

#[fallback]
pub async fn not_found() -> Response {
    Response::not_found()
}

let mut bp = Blueprint::new();
// The generated constant is named `NOT_FOUND`
bp.fallback(NOT_FOUND);

Using a custom identifier:

use pavex::{fallback, Blueprint, Response};

#[fallback(id = "CUSTOM_404")]
//         👆 Custom identifier
pub async fn not_found() -> Response {
    Response::not_found()
}

let mut bp = Blueprint::new();
// Use the custom identifier when registering
bp.fallback(CUSTOM_404);