-
-
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
local scope variables are not available inside rescue_from #1922
Comments
I think I'd want this to work. Doing As a workaround, the rescue block is not executed in the same scope as the request, so you don't have access to attributes. You do have access to |
@dblock For now I worked it around in my code by following this merged code. But once, it will be released I think I can use
def signed_in_user
# check https://github.com/ruby-grape/grape/pull/1918/files
# We will remove it, once the grape 1.5 is released.
env[Grape::Env::API_ENDPOINT].current_user
end |
Yep. I will leave this open to make @aruprakshit This is a good place for someone to write some tests and contribute :) Wink wink |
Hey 👋 I was taking a look at this issue and after some checks I can see that we are in a different instance when we are inside the require 'grape'
class MyAPI < Grape::API
class MyError < StandardError; end
before do
puts "self => #{self.class}"
@my_var = 328
end
rescue_from MyError do |e|
puts "self => #{self.class}"
puts "my var value => #{@my_var}"
error!({ my_var: @my_var }, 401)
end
get '/wadus' do
puts "my var value => #{@my_var}"
puts "self => #{self.class}"
raise MyError.new
end
end When entering on the This means that we cannot have access to the same instance variables declared in the I have prepared a PR with some failing tests expecting that when rescuing, the error contains the same value that we set in a instance variable in the For the solution I have a little idea. My feeling is that when performing run_rescue_handler instead of performing self => #<Class:0x000000010448a148>
my var value => 328
self => #<Class:0x000000010448a148>
self => #<Class:0x000000010448a148>
my var value => 328 However, I'm getting this error because as I'm executing the handler in the endpoint instance, the UncaughtThrowError: uncaught throw :error
/Users/Juancarlos.Garcia/code/grape/lib/grape/dsl/inside_route.rb:167:in `throw'
/Users/Juancarlos.Garcia/code/grape/lib/grape/dsl/inside_route.rb:167:in `error!'
/Users/Juancarlos.Garcia/code/grape-2236/api.rb:14:in `block in <class:MyAPI>'
/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/error.rb:135:in `instance_exec' When using the Also, some other methods are failing in the tests following this approach, as the user can use methods like As an alternative (and as a test) I just copied the instance variables from the endpoint instance into the new instance like endpoint.instance_variables.each do |variable|
instance_variable_set(variable, endpoint.instance_variable_get(variable))
end With this, and performing the self => #<Class:0x0000000105839d88>
my var value => 328
self => #<Class:0x0000000105839d88>
self => #<Class:0x00000001028379f0>
my var value => 328 I think that the first approach is the ideal, because we are performing the handler in the same context of the endpoint. However, same method What do you think about the second approach @dblock ? Can you give me some light here? :) Happy to hear from someone else as well :) |
…endpoints when rescue_from
Thanks for the detailed explanation! I don't love copying instance variables at all because modifying them will not work across copies, so I think fixing |
…the instance variables behavior. Extra tests added
…e run_rescue_handler method
…inside rescue_from (#2377) * fix(#1922): Allow to use instance variables defined in the endpoints when rescue_from * fix(#1922): Update CHANGELOG and running rubocop * fix(#1922): Updating UPGRADING and README files explaining the instance variables behavior. Extra tests added * fix(#1922): Send endpoint parameter as the last param of the run_rescue_handler method * fix(#1922): Adding short before/after example to UPGRADING * fix(#1922): Fixing CHANGELOG format style
I have current_user defined inside the before hook, and then we try to read it from
rescue_from
method, we getnil
.We get
nil
error whencurrent_user
being accessed inside therescue_from
block ascurrent_user.id
. How can I make availablecurrent_user
inside therescue_from
block?Gemfile
The text was updated successfully, but these errors were encountered: