warp icon indicating copy to clipboard operation
warp copied to clipboard

Param filter with user-defined rejection

Open hwchen opened this issue 6 years ago • 7 comments
trafficstars

updates filters::param2:

  • changes name to param_with_err. This still doesn't quite feel right to me, so let me know if there's other suggestions.
  • returns Rejection directly from FromStr Err, instead converting the Err to a Cause and always returning a not found Rejection.
  • created a fuller example in the docstring.

Let me know if this is the idea you had from #255.

hwchen avatar Oct 28 '19 16:10 hwchen

Playing around with this a bit, I'm not sure it makes sense to have users implement their own Err -> Rejection if Causes are going to be the main way of identifying errors in a recover. Also, from notes in the api, it looks like you're moving towards using custom rejections more generally.

So, perhaps it makes more sense to have param_with_err just return a custom rejection, instead of having the user define an extra impl that's not really that powerful.

hwchen avatar Oct 28 '19 23:10 hwchen

I'm not sure it makes sense to have users implement their own Err -> Rejection if Causes are going to be the main way of identifying errors in a recover.

I'm not quite sure what you mean by this. However, relatedly, master has just seen a refactor of the rejection system which would affect this (#311).

seanmonstar avatar Nov 12 '19 23:11 seanmonstar

Ok, I'll look at the new rejection system.

hwchen avatar Nov 13 '19 21:11 hwchen

I've rebased on master. In this implementation, the user just has to implement reject::Reject for their error, then they can use it in param_with_err (formerly param2). Let me know if you have any questions on this now.

hwchen avatar Nov 15 '19 23:11 hwchen

And it seems like you no longer need to implement std::error::Error to work with the custom rejection system, just reject::Reject.

hwchen avatar Nov 15 '19 23:11 hwchen

It's been a while since this PR was submitted, I've updated it to better reflect the latest Rejection system.

Quick summary:

path::param will always return reject::not_found() if there's an error in parsing. path::param_with_reject will allow the user to specify a custom rejection in case of parsing failure.

I see that param2, which served this function previously (I think), was removed. Let me know if there's no longer interest and I can close this PR.

hwchen avatar Apr 22 '20 22:04 hwchen

Hi, and sorry for the delay! So if I understand correctly what you are trying to achieve is being able to replace a Known rejection with a custom one right? If so can't you do that with a recover handler and find and return the response you want on that case

jxs avatar Jun 30 '21 22:06 jxs