【问题标题】:access params in rescue_from在rescue_from 中访问参数
【发布时间】:2014-10-25 19:35:08
【问题描述】:

我正在使用葡萄,我想访问rescue_from中的请求参数:

class API < Grape::API

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

我该怎么做?

【问题讨论】:

    标签: ruby-on-rails rack grape


    【解决方案1】:

    我设法做到了:

    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
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

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

      婴儿车应该可以在 helper 中使用,但我不确定这个技巧 https://github.com/intridea/grape/issues/438

      【讨论】:

        【解决方案3】:

        如果有人仍然对此感到疑惑,请提供更新的答案:

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

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

        【讨论】:

          猜你喜欢
          • 2016-05-11
          • 2021-10-28
          • 2017-05-29
          • 2014-06-13
          • 2010-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-19
          相关资源
          最近更新 更多