Warp: Rejection

Hi,

i am struggling to understand rejections in warp.

I have a function that compiles, like:

pub fn reply(store: Store) -> impl Filter<Extract = impl warp::Reply, Error = warp::reject::Rejection> + Clone { ...

I now want to handle the case that some entry is not found in the store in the sense of

Response::builder().status(StatusCode::BAD_REQUEST).body(error_string)

I am still not sure how to construct a rejection. When i start with

if error { return Err(warp::reject::not_found()) }

the compiler complains:

the trait std::clone::Clone is not implemented for warp::reject::Rejection
the trait warp::filter::FilterBase is not implemented for std::result::Result<_, warp::reject::Rejection>

What is the best way to construct a Filter that returns a JSON error string and HTTP error code?

Wow, the documentation is not easy to follow, things that appear to be central important types such as FilterBase appear to be completely hidden from the documentation. Even after cloning warp locally and building with --document-private-items, I still couldn't figure out how things fit together.

It seems like you shouldn't be returning Err, but I can't tell what you should be using to wrap the rejection instead.

2 Likes

Sounds like a good time to raise an issue, hopefully get an answer and improve the documentation for others.

I haven't used warp yet but am considering it for a personal project, so interested in how this pans out :slight_smile: (I expect well)

Thank you. I agree that there are some gaps in warp's documentation.
I am also sad that the traits in the return types do not work well together with todo!() or unimplemented!().

But warp is basically a one man show and Sean is insanely productive across many important projects.
So i try to get comfortable using warp and maybe contribute to its documentation later on, but all this without diverting Sean's energy too much.

Hm, rejections do not seem to be the right mean: https://github.com/seanmonstar/warp/issues/388

Rejections are meant to say a Filter couldn't fulfill its preconditions, but maybe another Filter can. If a filter is otherwise fully matched, and an error occurs in your business logic, it's probably not correct to reject with the error. In that case, you'd want to construct a Reply that describes your error.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.