Type Alias lexa_framework::response::Result

pub type Result<T, E = ErrorResponse> = Result<T, E>;
Expand description

An IntoResponse-based result type that uses ErrorResponse as the error type.

All types which implement IntoResponse can be converted to an ErrorResponse. This makes it useful as a general purpose error type for functions which combine multiple distinct error types that all implement IntoResponse.

Example

use axum::{
    response::{IntoResponse, Response},
    http::StatusCode,
};

// two fallible functions with different error types
fn try_something() -> Result<(), ErrorA> {
    // ...
}

fn try_something_else() -> Result<(), ErrorB> {
    // ...
}

// each error type implements `IntoResponse`
struct ErrorA;

impl IntoResponse for ErrorA {
    fn into_response(self) -> Response {
        // ...
    }
}

enum ErrorB {
    SomethingWentWrong,
}

impl IntoResponse for ErrorB {
    fn into_response(self) -> Response {
        // ...
    }
}

// we can combine them using `axum::response::Result` and still use `?`
async fn handler() -> axum::response::Result<&'static str> {
    // the errors are automatically converted to `ErrorResponse`
    try_something()?;
    try_something_else()?;

    Ok("it worked!")
}

As a replacement for std::result::Result

Since axum::response::Result has a default error type you only have to specify the Ok type:

use axum::{
    response::{IntoResponse, Response, Result},
    http::StatusCode,
};

// `Result<T>` automatically uses `ErrorResponse` as the error type.
async fn handler() -> Result<&'static str> {
    try_something()?;

    Ok("it worked!")
}

// You can still specify the error even if you've imported `axum::response::Result`
fn try_something() -> Result<(), StatusCode> {
    // ...
}

Aliased Type§

enum Result<T, E = ErrorResponse> {
    Ok(T),
    Err(E),
}

Variants§

§1.0.0

Ok(T)

Contains the success value

§1.0.0

Err(E)

Contains the error value

Trait Implementations§

§

impl<S, T, B> FromRequest<S, B, ViaRequest> for Result<T, <T as FromRequest<S, B, ViaRequest>>::Rejection>where T: FromRequest<S, B, ViaRequest>, B: Send + 'static, S: Send + Sync,

§

type Rejection = Infallible

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
§

fn from_request<'life0, 'async_trait>( req: Request<B>, state: &'life0 S ) -> Pin<Box<dyn Future<Output = Result<Result<T, <T as FromRequest<S, B, ViaRequest>>::Rejection>, <Result<T, <T as FromRequest<S, B, ViaRequest>>::Rejection> as FromRequest<S, B, ViaRequest>>::Rejection>> + Send + 'async_trait, Global>>where 'life0: 'async_trait, Result<T, <T as FromRequest<S, B, ViaRequest>>::Rejection>: 'async_trait,

Perform the extraction.
§

impl<S, T> FromRequestParts<S> for Result<T, <T as FromRequestParts<S>>::Rejection>where T: FromRequestParts<S>, S: Send + Sync,

§

type Rejection = Infallible

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
§

fn from_request_parts<'life0, 'life1, 'async_trait>( parts: &'life0 mut Parts, state: &'life1 S ) -> Pin<Box<dyn Future<Output = Result<Result<T, <T as FromRequestParts<S>>::Rejection>, <Result<T, <T as FromRequestParts<S>>::Rejection> as FromRequestParts<S>>::Rejection>> + Send + 'async_trait, Global>>where 'life0: 'async_trait, 'life1: 'async_trait, Result<T, <T as FromRequestParts<S>>::Rejection>: 'async_trait,

Perform the extraction.
source§

impl<B, E> IntoMapRequestResult<B> for Result<Request<B>, E>where E: IntoResponse,

source§

fn into_map_request_result( self ) -> Result<Request<B>, Response<UnsyncBoxBody<Bytes, Error>>>

Perform the conversion.
§

impl<T, E> IntoResponse for Result<T, E>where T: IntoResponse, E: IntoResponse,

§

fn into_response(self) -> Response<UnsyncBoxBody<Bytes, Error>>

Create a response.
§

impl<T> IntoResponse for Result<T, ErrorResponse>where T: IntoResponse,

§

fn into_response(self) -> Response<UnsyncBoxBody<Bytes, Error>>

Create a response.