简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM