Boti
Boti

Reputation: 3435

access params in rescue_from

I am using grape and I would like to access the request params within the rescue_from:

class API < Grape::API

  rescue_from Grape::Exceptions::ValidationErrors do |e|
    rack_response({
  end
...

How can I do that?

Upvotes: 2

Views: 1694

Answers (4)

Leo
Leo

Reputation: 1619

context.params seems to be the way to do this in 2023 - from the Grape Wiki on exception handling:

Inside the rescue_from block, the environment of the original controller method(.self receiver) is accessible through the #context method.

e.g.

rescue_from :all do |e|
  user_id = context.params[:user_id]
  ...
end

Upvotes: 0

foerever
foerever

Reputation: 335

a bit more recent answer in case anybody is still wondering about this:

rescue_from Grape::Exceptions::ValidationErrors do |e|
  env['grape.request'].params
end

see https://github.com/ruby-grape/grape/pull/894

Upvotes: 0

Boti
Boti

Reputation: 3435

I managed to do with this:

rescue_from :all do |e|
  req = Rack::Request.new(env)
  ApiCallAudits.create data: {input_params: req.params.as_json}, backtrace: $!.to_s, status: :error
end

Upvotes: 5

Igor Guzak
Igor Guzak

Reputation: 2165

you could try something like this:

rescue_from  Grape::Exceptions::ValidationErrors do |e|
  env['api.endpoint'].helper_method
end

prams should be available in helper, but I'm not sure about this trick https://github.com/intridea/grape/issues/438

Upvotes: 3

Related Questions