Skip to content

local scope variables are not available inside rescue_from #1922

Open
@aruprakshit

Description

@aruprakshit

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'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions