Trait anyhow_http::ResultExt

source ·
pub trait ResultExt {
    type Item;

    // Required methods
    fn map_status(
        self,
        status_code: StatusCode,
    ) -> StdResult<Self::Item, HttpError>;
    fn map_http_error<S>(
        self,
        status_code: StatusCode,
        reason: S,
    ) -> StdResult<Self::Item, HttpError>
       where S: Into<Cow<'static, str>>;
}
Expand description

Extension trait to map the error variant of a Result to a HttpError.

Required Associated Types§

Required Methods§

source

fn map_status(self, status_code: StatusCode) -> StdResult<Self::Item, HttpError>

Maps a Result<T, E> to Result<T, HttpError<R>> by creating a HttpError with the specified status code wrapping the error contained Err.

§Example
use http::StatusCode;
use anyhow_http::{http_error, HttpError, ResultExt};

let s: Result<i32, HttpError> = "nan"
    .parse::<i32>()
    .map_status(StatusCode::BAD_REQUEST);
assert_eq!(s, Err(http_error!(BAD_REQUEST)));
source

fn map_http_error<S>( self, status_code: StatusCode, reason: S, ) -> StdResult<Self::Item, HttpError>
where S: Into<Cow<'static, str>>,

Maps a Result<T, E> to Result<T, HttpError<R>> by creating a HttpError with the specified status code and reason wrapping the error contained Err.

§Example
use http::StatusCode;
use anyhow_http::{http_error, HttpError, ResultExt};

let s: Result<i32, HttpError> = "nan"
    .parse::<i32>()
    .map_http_error(StatusCode::BAD_REQUEST, "invalid number");
assert_eq!(s, Err(http_error!(BAD_REQUEST, "invalid number")));

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<E, T> ResultExt for Result<T, E>
where E: Into<Error> + Send + Sync + 'static,

source§

type Item = T

source§

fn map_status(self, status_code: StatusCode) -> StdResult<T, HttpError>

source§

fn map_http_error<S>( self, status_code: StatusCode, reason: S, ) -> StdResult<Self::Item, HttpError>
where S: Into<Cow<'static, str>>,

Implementors§