FromRequest for Option<T> should fail if from_request(...) T fails, not be None #2871
Open
2 tasks done
Labels
suggestion
A suggestion to change functionality
API Docs to Existing Functionality
https://api.rocket.rs/master/rocket/request/trait.FromRequest
Problems with Existing Functionality
When using the FromRequest guards/extractors with an Option, there's no differentiation between a good and a bad request.
Imagine the following struct:
Let's imagine we have a handler which can handle the 'Session-Id' header if it is sent in, but can function without:
In the current semantics for Option where T: FromRequest, the request will succeed, even with a malformed header, passed in as None. If I were a client of that API, I'd find it highly surprising.
Suggested Changes
I would suggest that the Option guard semantics were changed to fail if Outcome::Error, mapping Forward to None. In other words, this would be how I'd implement it:
That way, we could keep the parse errors and whatnot as errors (and not silently suppress them). This would compose well with catchers.
Alternatives Considered
In the current solution, the only alternative for a handler would be to use the an Outcome guard, like this:
This adds extra complexity and forces boilerplate error handling code as well as Rocket-specific type use into the handlers.
Additional Context
In #2867 I've implemented typed headers (see #1067). This would fit well with these headers, like Option, etc.
I have a patch ready.
System Checks
I do not believe that this suggestion can or should be implemented outside of Rocket.
I was unable to find a previous suggestion for this change.
The text was updated successfully, but these errors were encountered: