Skip to main content

try_get_request_error_data

Attribute Macro try_get_request_error_data 

Source
#[try_get_request_error_data]
Expand description

Extracts request error data into a variable wrapped in Option type.

This attribute macro retrieves request error information if an error occurred during handling and makes it available as an Option variable. The extracted value is wrapped in an Option type to safely handle cases where no error occurred.

§Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/try_get_request_error_data")]
struct RequestErrorDataOptionTest;

impl ServerHook for RequestErrorDataOptionTest {
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    #[response_body(&format!("Request error data: {try_get_request_error_data:?}"))]
    #[try_get_request_error_data(try_get_request_error_data)]
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

impl RequestErrorDataOptionTest {
    #[try_get_request_error_data(try_get_request_error_data)]
    async fn try_get_request_error_data_with_ref_self(&self, stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

#[try_get_request_error_data(try_get_request_error_data)]
async fn standalone_try_get_request_error_data_handler(stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }

The macro accepts a variable name that will contain the request error data. The variable will be available as an Option<RequestError> in the function scope.

§Multi-Parameter Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/try_get_request_error_data")]
struct MultiRequestErrorDataOption;

impl ServerHook for MultiRequestErrorDataOption {
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    #[response_body(&format!("error1: {error1:?}, error2: {error2:?}"))]
    #[try_get_request_error_data(error1, error2)]
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

The macro accepts multiple variable names separated by commas.