Grape:从无效的JSON中拯救

时间:2015-03-02 14:07:12

标签: ruby-on-rails ruby json rspec grape

首先

我用葡萄来构建我的API(Rails 4)。当有人发送无效的JSON正文时(例如忘记了上一个}),会引发以下错误:

ActionDispatch::ParamsParser::ParseError (795: unexpected token at '{"foobar": 1234

')

我尝试用葡萄rescue_from :all选项,但这不起作用。在堆栈跟踪中,我没有看到涉及的葡萄宝石。似乎这个错误是从actionpack抛出的:

  .gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/params_parser.rb:53:in `rescue in parse_formatted_parameters'
  .gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/params_parser.rb:32:in `parse_formatted_parameters'
  .gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/params_parser.rb:23:in `call'

但是,捕获这些错误的最佳方法是什么,返回400: Bad Request错误,并在json响应中包含unexpected token at '{"foobar": 1234消息?

第二

我试图用RSpec测试这个,但是在发送带有无效JSON的原始请求时没有任何好运。我用

试了一下
post "/my_route", '{some invalid json'

但是这并没有从上面抛出错误。我认为自从Rails 4以来,作为字符串传递的第二个参数被视为原始体?

1 个答案:

答案 0 :(得分:5)

不幸的是,ActionDispatch在进入控制器之前运行得很好,因此您无法使用Grape(AFAIK)执行此操作。

我们也遇到了这个问题,并在Thoughtbot上发现了wonderful article这个问题。

使用Curb gem进行多次调用:

require 'curb'
it 'sends poorly formatted json' do
  broken_json = %Q|{"notice":{"title":"A sweet title"#{invalid_tokens}}}|
  resp = Curl.post("http://#{host}:#{port}#{path}", broken_json)

  expect(resp.response_code).to eq 500
end

Thoughtbot建议编写一个中间件类来捕获未来的JSON解析错误,如下所示:

# in app/middleware/catch_json_parse_errors.rb
class CatchJsonParseErrors
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call(env)
    rescue ActionDispatch::ParamsParser::ParseError => error
      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
    end
  end
end