Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

http - How do I handle errors in Warp using both Rejection and the question-mark operator?

Using warp.rs 0.2.2, let's consider a basic web service with one route for GET /:

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let getRoot = warp::get().and(warp::path::end()).and_then(routes::getRoot);
    warp::serve(getRoot).run(([0, 0, 0, 0], 3030)).await;
    Ok(())
}

My goal is to use ? for error handling in the route handlers, so let's write one that can error and return early in crate::routes:

use crate::errors::ServiceError;
use url::Url;

pub async fn getRoot() -> Result<impl warp::Reply, warp::Rejection> {
    let _parsed_url = Url::parse(&"https://whydoesn.it/work?").map_err(ServiceError::from)?;

    Ok("Hello world !")
}

This version works. Here the error that's returned by Url::parse() is a url::ParseError

To convert between error types, from url::ParseError to ServiceError, then from ServiceError to warp::Rejection, I've written some error helpers in crate::errors:

#[derive(thiserror::Error, Debug)]
pub enum ServiceError {
    #[error(transparent)]
    Other(#[from] anyhow::Error), // source and Display delegate to anyhow::Error
}
impl warp::reject::Reject for ServiceError {}
impl From<ServiceError> for warp::reject::Rejection {
    fn from(e: ServiceError) -> Self {
        warp::reject::custom(e)
    }
}
impl From<url::ParseError> for ServiceError {
    fn from(e: url::ParseError) -> Self {
        ServiceError::Other(e.into())
    }
}

Now, the above works, and I'm trying to shorten the second code block to use ? for error handling directly, and convert automatically from the underlying error (here url::ParseError) to a warp::Rejection. Here's what I've tried:

use crate::errors::ServiceError;
use url::Url;

pub async fn getRoot() -> Result<impl warp::Reply, ServiceError> {
    let _parsed_url = Url::parse(&"https://whydoesn.it/work?")?;

    Ok("Hello world !")
}

The url::ParseError returned by Url::Parse will convert fine into a ServiceError to return, but returning a ServiceError from my handler doesn't work. The first compilation error I get is:

error[E0277]: the trait bound `errors::ServiceError: warp::reject::sealed::CombineRejection<warp::reject::Rejection>` is not satisfied
   --> src/main.rs:102:54
    |
102 |     let getRoot = warp::get().and(warp::path::end()).and_then(routes::getRoot);
    |                                                      ^^^^^^^^ the trait `warp::reject::sealed::CombineRejection<warp::reject::Rejection>` is not implemented for `errors::ServiceError`

Is there a way I can keep the short error handling using ? only and either:

  • make ServiceError implement warp::reject::sealed::CombineRejection<warp::reject::Rejection> ?
  • work around that ?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can implement From to convert your error type into warp::Rejection using reject::custom. Rejection encapsulates custom types which you can later choose to inspect inside of a recover handler.

This example uses a plain error struct, but if you have an error enum you can match on the variants inside the recovery handler and perform different logic as needed.

use serde::Deserialize;
use snafu::{ensure, Snafu};
use std::convert::Infallible;
use warp::{
    filters::{any, query, BoxedFilter},
    http::StatusCode,
    reject::Reject,
    Filter, Rejection, Reply,
};

// A normal error type, created by SNAFU
#[derive(Debug, Snafu)]
#[snafu(display("Expected a value less than 10, but it was {}", value))]
struct LessThanTenError {
    value: i32,
}

// A function that might fail
fn validate(value: i32) -> Result<i32, LessThanTenError> {
    ensure!(value < 10, LessThanTenContext { value });
    Ok(value)
}

// We need a custom type to later extract from the `Rejection`. In
// this case, we can reuse the error type itself.
impl Reject for LessThanTenError {}

// To allow using `?`, we implement a conversion from our error to
// `Rejection`
impl From<LessThanTenError> for Rejection {
    fn from(other: LessThanTenError) -> Self {
        warp::reject::custom(other)
    }
}

#[tokio::main]
async fn main() {
    let api = simple_math().recover(report_invalid);

    let p: std::net::SocketAddr = "0.0.0.0:8888".parse().unwrap();
    warp::serve(api).run(p).await;
}

#[derive(Debug, Deserialize)]
struct QueryParams {
    a: i32,
    b: i32,
}

fn simple_math() -> BoxedFilter<(impl Reply,)> {
    any::any()
        .and(query::query())
        .and_then(|args: QueryParams| async move {
            // Look at us using those question marks!
            let a = validate(args.a)?;
            let b = validate(args.b)?;
            let sum = validate(a + b)?;

            // We specify that we are returning an error type of
            // `Rejection`, which allows the compiler to know what
            // type to convert to when using `?` here.
            Ok::<_, Rejection>(format!("The sum is {}", sum))
        })
        .boxed()
}

async fn report_invalid(r: Rejection) -> Result<impl Reply, Infallible> {
    if let Some(e) = r.find::<LessThanTenError>() {
        // It was our specific error type, do whatever we want. We
        // will just print out the error text.
        Ok(warp::reply::with_status(
            e.to_string(),
            StatusCode::BAD_REQUEST,
        ))
    } else {
        // Do prettier error reporting for the default error here.
        Ok(warp::reply::with_status(
            String::from("Something bad happened"),
            StatusCode::INTERNAL_SERVER_ERROR,
        ))
    }
}
[dependencies]
serde = { version = "1.0.118", features = ["derive"] }
snafu = "0.6.10"
tokio = { version = "0.2.23", features = ["full"] }
warp = "0.2.5"
% curl 'http://127.0.0.1:8888'
< HTTP/1.1 500 Internal Server Error
Something bad happened

% curl -v 'http://127.0.0.1:8888?a=1&b=2'
< HTTP/1.1 200 OK
The sum is 3

% curl -v 'http://127.0.0.1:8888?a=6&b=5'
< HTTP/1.1 400 Bad Request
Expected a value less than 10, but it was 11

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...