-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method-not-allowed fallback on the Router level #2251
Comments
I wonder if we should just apply the normal |
Wdym by keeping the allow header? You mean setting it if the user-provided fallback handler / service hasn't set it? What about the status code, currently it's also the fallback handler / service's task to set it to 404 / 405 if appropriate. |
Hm that's a good point. Didn't consider that 🤔 Not sure how to deal with that. It feels like it should still be possible to set the Allow header even if using something like |
I would like to pitch in on this, this issue is something that I'm currently coming across and it's rather annoying to not have a good alternative that will cover the method not found attribute. For a little bit of context, our API implementation is trying to return JSON for most of the common client errors (the fallback handler currently covers the 404 case). Having either a |
I am using axum as an api backend, I hope this issue could be solved. |
Hey there, I would volunteer to work on this, if it is still relevant. |
It is. One recommendation regarding the implementation: it might be good to only set the Allow header if the fallback service didn't set it. But we can further discuss that on the PR if there's any questions around that, not important to get that bit right immediately. |
@jplatte What do you think would be the most elegant way on implementing it? Im familiar with axum, but this is my first contribution and im not that well versed in the internals. My first intuition would be to go with |
I haven't looked at this code in a while, but I'd try to make |
I was also looking for a way to return a JSON response to method-not-allowed rejections, so I'll be interested in whatever PR comes out of this. |
Sorry for keeping you waiting, wasn't available for the past two weeks. Ive pushed an attempt of mine to implement it, the example from OP works as expected now, although the CI pipeline is currently failing. Feedback would be appreciated as I am currently not confident in opening a real PR for this 😅 |
That PR is as real as PRs get 😉 |
Hi there @jplatte, sorry for letting this fall behind for so long. CI is passing now, would be nice if you could give some feedback. |
No worries. I should have probably left some useful comments earlier already, before you came back to fix CI. |
Feature Request
Motivation
It's a common mistake to expect
Router::fallback
to be triggered on method-not-allowed.Furthermore, as shown by #1005, it is pretty easy to misinterpret the current behavior for a bug.
While returning HTTP 405 is normal in this case, it would be helpful to have a simple baked-in solution to handle it, in a way similar to fallback.
Proposal
Add a
Router::method_not_allowed_fallback
method that allows defining a fallback for this specific situationUsage
Alternatives
An included middleware such as
MethodNotAllowedLayer(fallback: Handler)
.Usage
Anyway, I think it would be great to mention this topic in the
Router::fallback
documentation.The text was updated successfully, but these errors were encountered: