Attribute Macro error_observer
#[error_observer]
Expand description
Define an error observer.
Error observers are invoked whenever an error occurs during request processing, allowing you to log errors, send them to monitoring services, or perform other observability tasks.
§Example
Log errors that occur during request processing:
use pavex::error_observer;
use tracing_log_error::log_error;
#[error_observer]
pub async fn error_logger(e: &pavex::Error) {
log_error!(e)
}
§Guide
Check out the “Errors” section of Pavex’s guide for a thorough introduction to errors in Pavex applications.
§Registration
Use Blueprint::error_observer
to register the error observer with your Blueprint
.
The #[error_observer]
macro generates a constant that you can use to refer to the
observer when invoking Blueprint::error_observer
.
§Arguments
The sections below provide an exhaustive list of all the arguments and flags supported by the error_observer
macro:
Name | Kind | Required |
---|---|---|
id | Argument | No |
§id
By default, Pavex generates a constant named after your function (converted to UPPER_SNAKE_CASE) that you use when registering the error observer.
The id
argument allows you to customize the name of the generated constant.
§Example
Using the default generated identifier:
use pavex::{error_observer, Blueprint};
#[error_observer]
pub async fn error_logger(e: &pavex::Error) {
// Log the error
}
let mut bp = Blueprint::new();
// The generated constant is named `ERROR_LOGGER`
bp.error_observer(ERROR_LOGGER);
Using a custom identifier:
use pavex::{error_observer, Blueprint};
#[error_observer(id = "MY_ERROR_LOGGER")]
// 👆 Custom identifier
pub async fn error_logger(e: &pavex::Error) {
// Log the error
}
let mut bp = Blueprint::new();
// Use the custom identifier when registering
bp.error_observer(MY_ERROR_LOGGER);