23t*_*tux 6 ruby json rspec ruby-on-rails ruby-grape
第一:
我正在使用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错误以及unexpected token at '{"foobar": 1234在json响应中包含消息的最佳方法是什么?
第二:
我尝试用RSpec测试这个,但是在发送带有无效JSON的原始请求时没有任何运气.我试过了
post "/my_route", '{some invalid json'
但这不会从上面抛出错误.我认为自Rails 4以来,作为字符串传递的第二个参数被视为原始体?
不幸的是,ActionDispatch在到达控制器之前就运行得非常好,因此您无法使用Grape(AFAIK)做到这一点。
我们也遇到了这个问题,并从Thoughtbot家伙那里找到了一篇很棒的文章。
使用Curb宝石进行紧急呼叫:
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
| 归档时间: | 
 | 
| 查看次数: | 1071 次 | 
| 最近记录: |