Type Alias actix_web::error::Result

source ·
pub type Result<T, E = Error> = Result<T, E>;
Expand description

A convenience Result for Actix Web operations.

This type alias is generally used to avoid writing out actix_http::Error directly.

Aliased Type§

enum Result<T, E = Error> {
    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§

source§

impl<T, E> FromRequest for Result<T, E>where T: FromRequest, T::Error: Into<E>,

Extract from the request, passing error type through to handler.

If the inner T::from_request returns an error, allow handler to receive the error rather than immediately returning an error response.

Examples

use actix_web::{web, dev, App, Result, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorBadRequest;
use futures_util::future::{ok, err, Ready};
use serde::Deserialize;
use rand;

#[derive(Debug, Deserialize)]
struct Thing {
    name: String
}

impl FromRequest for Thing {
    type Error = Error;
    type Future = Ready<Result<Thing, Error>>;

    fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
        if rand::random() {
            ok(Thing { name: "thingy".into() })
        } else {
            err(ErrorBadRequest("no luck"))
        }
    }
}

/// extract `Thing` from request
async fn index(supplied_thing: Result<Thing>) -> String {
    match supplied_thing {
        Ok(thing) => format!("Got thing: {:?}", thing),
        Err(e) => format!("Error extracting thing: {}", e)
    }
}

let app = App::new().service(
    web::resource("/users/:first").route(web::post().to(index))
);
§

type Error = Infallible

The associated error which can be returned.
§

type Future = FromRequestResFuture<<T as FromRequest>::Future, E>

Future that resolves to a Self. Read more
source§

fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future

Create a Self from request parts asynchronously.
source§

fn extract(req: &HttpRequest) -> Self::Future

Create a Self from request head asynchronously. Read more
source§

impl<R, E> Responder for Result<R, E>where R: Responder, E: Into<Error>,

§

type Body = EitherBody<<R as Responder>::Body, BoxBody>

source§

fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>

Convert self to HttpResponse.
source§

fn customize(self) -> CustomizeResponder<Self>where Self: Sized,

Wraps responder to allow alteration of its response. Read more