The Wayback Machine - https://web.archive.org/web/20210106160148/https://github.com/ruby-grape/grape/issues/1922
Skip to content
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

Open
aruprakshit opened this issue Oct 23, 2019 · 3 comments
Open

local scope variables are not available inside rescue_from #1922

aruprakshit opened this issue Oct 23, 2019 · 3 comments

Comments

@aruprakshit
Copy link

@aruprakshit aruprakshit commented Oct 23, 2019

I have current_user defined inside the before hook, and then we try to read it from rescue_from method, we get nil.

before do
  token         = nil
  token         = headers.fetch("Authorization", "").split("Basic ")[1] unless report_url?
  @current_user = ::User.find_by(auth_token: token) if token.present?

  PaperTrail.request.whodunnit = @current_user.id if @current_user
end

helpers Pundit

helpers do
  attr_reader :current_user

  def report_url?
    request.path_info == "/v1/share_reports/#{request.params['token']}"
  end

  # ....
end


rescue_from Pundit::NotAuthorizedError do |e|
  Airbrake.notify(e, {
    user: {
      id: current_user.id,
      email: current_user.email
    }
  })

  error!(
    {
      messages: {
        userNotification: e.message
      }
    }, 401
  )
end

We get nil error when current_user being accessed inside the rescue_from block as current_user.id. How can I make available current_user inside the rescue_from block?

Gemfile

gem "rails", "5.2.3"
gem 'grape', '~> 1.0'
gem 'grape-entity', '~> 0.7.1'
@dblock dblock added the bug? label Oct 23, 2019
@dblock
Copy link
Member

@dblock dblock commented Oct 23, 2019

I think I'd want this to work. Doing @something = in before makes it available in the execution of the API (we made that work a long time ago), but not in rescue_from. Let's call it a bug or a non-feature. Maybe someone could try to fix it?

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 context (since #1918, in a released version you can reach into ENV), which gives you the endpoint and then gives you endpoint-based context and settings. I think you generally want to do what Warden does, which is using ENV to pass around something across various contexts of the request (before, during, after) and not @.

@aruprakshit
Copy link
Author

@aruprakshit aruprakshit commented Oct 23, 2019

@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 context.

current_user is available when I do access it like below inside the helpers block.

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
@dblock
Copy link
Member

@dblock dblock commented Oct 23, 2019

Yep. I will leave this open to make @variable work. At the very least we should document what the scope of @ is in a rescue_from block.

@aruprakshit This is a good place for someone to write some tests and contribute :) Wink wink

@dblock dblock changed the title helpers are not available inside the rescue_from block - Why? local scope variables are not available inside rescue_from Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.