-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Change behaviors of rescue_from to not depend on mount order #1975
Comments
Can you turn it into a spec? Looks incorrect to me. |
@dblock Should I create a PR with a spec? Or gist in the discussion would be enough? |
Yes. And don't hesitate to try a fix! |
Hey guys! 👋 I've been examining this issue and I'm uncertain if it qualifies as a bug. As far as I can tell, Grape seems to be functioning as intended. In your What's happening here is that upon mounting, you essentially "merge" the Consequently, when an error occurs, the first error handlers considered are those within the class. If none apply, it then resorts to the grape/lib/grape/middleware/error.rb Lines 43 to 46 in 3901bf4
In your scenario, the initial entry point is By altering the order of mounting before the With that being said, I still maintain the view that this behavior is not a bug; rather, it aligns with the expected functionality. What do you think? Make sense? |
Thanks for the deep dive! If you compare this behavior with |
Hi @dblock , thanks for the answer! I believe the functionality of the class PublicApi < Grape::API
rescue_from ActiveRecord::RecordNotFound do
binding.pry
error_response(status: 403)
end
rescue_from :all do
error_response(status: 403)
end
post :request do
User.find_by!(phone: phone)
end
end In this scenario, there are two Regarding the class PublicApi < Grape::API
version 'v1'
version 'v2'
post :request do
User.find_by!(phone: phone)
end
end Here, all endpoints after A comparison between the class PublicApi < Grape::API
rescue_from ActiveRecord::RecordNotFound do
binding.pry
error_response(status: 403)
end
mount AuthController
end
class AuthController < Grape::API
rescue_from ActiveRecord::RecordNotFound do
error_response(status: 403)
end
post :request do
User.find_by!(phone: phone)
end
end This translates to: class PublicApi < Grape::API
rescue_from ActiveRecord::RecordNotFound do
binding.pry
error_response(status: 403)
end
rescue_from ActiveRecord::RecordNotFound do
error_response(status: 403)
end
post :request do
User.find_by!(phone: phone)
end
end In this case, the last error handler defined takes precedence. This behavior aligns with the functionality of the Does it make sense? UPDATE: What I'm start thinking is that the real bug is the "working scenario" described by @wowinter13 about changing the order of the class PublicApi < Grape::API
mount AuthController
rescue_from ActiveRecord::RecordNotFound do
binding.pry
error_response(status: 403)
end
end
class AuthController < Grape::API
rescue_from :all do
error_response(status: 403)
end
post :request do
User.find_by!(phone: phone)
end
end It should be translated to: class PublicApi < Grape::API
rescue_from :all do
error_response(status: 403)
end
post :request do
User.find_by!(phone: phone)
end
rescue_from ActiveRecord::RecordNotFound do
binding.pry
error_response(status: 403)
end
end And it should follow the strategy for selecting the error handler and enter first in the |
I think you're completely correct on all the points above. So, is the problem in this issue correctly formulated or do we need to edit it? What's the tl;dr of the ask? What do we intend to change? Either way, before changing anything, we have to start by ensuring that we have specs covering all current behavior and documentation that expresses it correctly. Appreciate any PRs! |
Hey @dblock , I think that the issue should be edited from the original title "An incorrect behavior of :rescue_from" to something like "Different behaviors of rescue_from method when changing mount order" The If you agree, let me work on this :) First trying to cover in specs the current behavior and documenting it and then, trying to solve the mount issue. |
Hi again @dblock , I was working on documenting the current behavior and to add some tests for these scenarios and I found that there is already an explanation about the inherited Lines 411 to 422 in 3901bf4
In fact, this was already managed in #646 some years ago. So my question now is... do we want to change how the inheritance works when using |
I renamed this to "Change behaviors of rescue_from to not depend on mount order", lmk if this can be improved.
Well, no. Personally the order is predictable and logical (once you understand it). But the OP, @wowinter13 was visibly confused, so it tells me that I could be wrong. I guess I'd be looking for some strong opinions from various people and code contributions that change the behavior! Happy to be the arbiter. |
… in any order when using `mount`
… in any order when using `mount`
… in any order when using `mount`
This has been solved at #2384 :) |
Maybe @wowinter13 wants to try it out from HEAD? |
Hi, found an interesting case when
rescue_from :all
should override outerrescue_from :error_class
15: rescue_from ActiveRecord::RecordNotFound do 15: binding.pry 16: error_response(status: 403) 17: end
For some reason, it seems to not working correctly. I suppose
AuthController rescue_from :all
to be called first.But if we mount AuthController above
rescue_from ActiveRecord::RecordNotFound
it would work correctly.So, does it look like a bug or additional info to documentation?
The text was updated successfully, but these errors were encountered: