Coder Social home page Coder Social logo

Comments (3)

jxs avatar jxs commented on May 4, 2024

Hi there,
I tried running this example:

#![deny(warnings)]
extern crate warp;
extern crate hyper;

use warp::Filter;
use hyper::{Response, StatusCode};


fn main() {
    let simple = warp::get(warp::path::index()).map(warp::reply);

    let errors = simple.or_else(|rejection: warp::Rejection| match rejection.status() {
        StatusCode::NOT_FOUND => {
            Ok(Response::builder()
                .status(404)
                .body("Not Found"))
        },
        _ => {
            // convert all?? others into just 400s
            Ok(Response::builder()
                .status(400)
                .body("i swallowed an error!"))
        }
    });

    warp::serve(simple).run(([127, 0, 0, 1], 3030));
}

and after having to annotate the closure parameter I get

error[E0271]: type mismatch resolving `<std::result::Result<std::result::Result<hyper::Response<&str>, warp::http::Error>, warp::Rejection> as futures::future::IntoFuture>::Item == (impl warp::Reply,)`
  --> examples/custom_errors.rs:12:25
   |
12 |     let errors = simple.or_else(|rejection: warp::Rejection| match rejection.status() {
   |                         ^^^^^^^ expected enum `std::result::Result`, found tuple
   |
   = note: expected type `std::result::Result<hyper::Response<&str>, warp::http::Error>`
              found type `(impl warp::Reply,)`

if I change the or_else return to match (impl warp::Reply,) I get

error[E0277]: the trait bound `(std::result::Result<hyper::Response<&str>, warp::http::Error>,): warp::Future` is not satisfied
  --> examples/custom_errors.rs:12:25
   |
12 |     let errors = simple.or_else(|rejection: warp::Rejection| match rejection.status() {
   |                         ^^^^^^^ the trait `warp::Future` is not implemented for `(std::result::Result<hyper::Response<&str>, warp::http::Error>,)`
   |
   = note: required because of the requirements on the impl of `futures::future::IntoFuture` for `(std::result::Result<hyper::Response<&str>, warp::http::Error>,)`

shouldn't or_else's output be just an IntoFuture type yielding the same item and error types. ?

from warp.

seanmonstar avatar seanmonstar commented on May 4, 2024

Yep, I started working on this just now, and ran into exactly what you noticed too. I may have goofed on the or_else type signature... Well, I guess it works as long as you return in the Ok case the exact same type, it just fails in this first example because the first type is erased (impl Reply), and I don't think you could make a new one in separate function. It'd work if this example were changed to return a concrete type.

Still, I think this means or_else is not actually the easiest way to catch errors and simply return a new reply. That means probably a new combinator is needed that allows returning a new type (and so the the filter would end up returning Either<A, B>). I'm leaning towards recover as the combinator...

from warp.

caolan avatar caolan commented on May 4, 2024

I also ran into this recently and ended up nesting my custom error handler somewhere to simplify the types. If you do implement a new combinator, I'd really like to see an example of handling e.g. 404 differently for different sub-paths. I couldn't figure that out using or_else.

from warp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.