【问题标题】:How to Rescue from ActionDispatch::ParamsParser::ParseError in Rails 4如何从 Rails 4 中的 ActionDispatch::ParamsParser::ParseError 中救援
【发布时间】:2013-03-05 17:53:57
【问题描述】:

Rails 4 添加了一个异常 ActionDispatch::ParamsParser::ParseError 异常,但由于它位于中间件堆栈中,因此它似乎无法在正常的控制器环境中被挽救。在 json API 应用程序中,我想以标准错误格式响应。

这个gist 展示了插入中间件进行拦截和响应的策略。按照这种模式,我有:

application.rb:

module Traphos
  class Application < Rails::Application
    ....
    config.middleware.insert_before ActionDispatch::ParamsParser, "JSONParseError"
 end
end

中间件是:

class JSONParseError
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call(env)
    rescue ActionDispatch::ParamsParser::ParseError => e
      [422, {}, ['Parse Error']]
    end
  end
end

如果我在没有中间件的情况下运行测试,我会得到(规范):

Failures:

  1) Photo update attributes with non-parseable json
     Failure/Error: patch update_url, {:description => description}, "CONTENT_TYPE" => content_type, "HTTP_ACCEPT" => accepts, "HTTP_AUTHORIZATION" => @auth
     ActionDispatch::ParamsParser::ParseError:
       399: unexpected token at 'description=Test+New+Description]'

这正是我所期望的(ParseError that I can't rescue_from)。

现在只需在上面的中间件中添加更改:

  2) Photo update attributes with non-parseable json
     Failure/Error: response.status.should eql(422)

       expected: 422
            got: 200

并且日志显示标准控制器操作正在执行并返回正常响应(尽管因为它没有收到任何参数,所以它没有更新任何东西)。

我的问题:

  1. 如何从 ParseError 中拯救并返回自定义响应。感觉我在正确的轨道上,但并不完全在那里。

  2. 我无法弄清楚为什么在引发并救援异常时,控制器操作仍会继续进行。

非常感谢您的帮助,--Kip

【问题讨论】:

  • 是否有其他中间件可以改变返回状态?你有没有用 pry 或其他东西进行调试?

标签: ruby-on-rails ruby exception params rescue


【解决方案1】:

这篇文章(同样来自 2013 年)thoughtbot 也涵盖了这个主题。仅当您请求 json 时,他们才会将响应放入此中间件服务中

if env['HTTP_ACCEPT'] =~ /application\/json/
    error_output = "There was a problem in the JSON you submitted: #{error}"
    return [
      400, { "Content-Type" => "application/json" },
      [ { status: 400, error: error_output }.to_json ]
    ]
else
 raise error
end

【讨论】:

    【解决方案2】:

    事实证明,在中间件堆栈的更上层,ActionDispatch::ShowExceptions 可以配置一个异常应用程序。

    module Traphos
      class Application < Rails::Application
        # For the exceptions app
        require "#{config.root}/lib/exceptions/public_exceptions"
        config.exceptions_app = Traphos::PublicExceptions.new(Rails.public_path)
      end
    end
    

    很大程度上基于我现在使用的 Rails 提供的一个:

    module Traphos
      class PublicExceptions
        attr_accessor :public_path
    
        def initialize(public_path)
          @public_path = public_path
        end
    
        def call(env)
          exception    = env["action_dispatch.exception"]
          status       = code_from_exception(env["PATH_INFO"][1..-1], exception)
          request      = ActionDispatch::Request.new(env)
          content_type = request.formats.first
          body         = {:status => { :code => status, :exception => exception.class.name, :message => exception.message }}
          render(status, content_type, body)
        end
    
        private
    
        def render(status, content_type, body)
          format = content_type && "to_#{content_type.to_sym}"
          if format && body.respond_to?(format)
            render_format(status, content_type, body.public_send(format))
          else
            render_html(status)
          end
        end
    
        def render_format(status, content_type, body)
          [status, {'Content-Type' => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
                    'Content-Length' => body.bytesize.to_s}, [body]]
        end
    
        def render_html(status)
          found = false
          path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
          path = "#{public_path}/#{status}.html" unless path && (found = File.exist?(path))
    
          if found || File.exist?(path)
            render_format(status, 'text/html', File.read(path))
          else
            [404, { "X-Cascade" => "pass" }, []]
          end
        end
    
        def code_from_exception(status, exception)
          case exception
          when ActionDispatch::ParamsParser::ParseError
            "422"
          else
            status
          end
        end
      end
    end
    

    在测试环境中使用它需要设置配置变量(否则你会在开发和测试中获得标准的异常处理)。所以为了测试我有(编辑为只有关键部分):

    describe Photo, :type => :api do
      context 'update' do
        it 'attributes with non-parseable json' do 
    
          Rails.application.config.consider_all_requests_local = false
          Rails.application.config.action_dispatch.show_exceptions = true
    
          patch update_url, {:description => description}
          response.status.should eql(422)
          result = JSON.parse(response.body)
          result['status']['exception'].should match(/ParseError/)
    
          Rails.application.config.consider_all_requests_local = true
          Rails.application.config.action_dispatch.show_exceptions = false
        end
      end
    end
    

    它以公共 API 方式按我的需要执行,并且适用于我可能选择自定义的任何其他异常。

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      相关资源
      最近更新 更多