gpt4 book ai didi

ruby-on-rails - Rails 3.2,处理损坏的请求体(MultiJson::DecodeError)

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:57 24 4
gpt4 key购买 nike

我正在开发 Rails 3.2.11 应用程序 (MRI 1.9.3)。
它的一部分是一个网络服务,它接收带有 JSON 序列化主体的 POST 请求。
一切正常,但我希望它能够适应格式错误的请求,例如无效的 JSON。

现在,如果它收到一个 JSON,比方说,缺少一个逗号,它将返回一个 500 错误和一个包含默认 Rails 错误 View 的 HTML 响应(加上开发时的回溯)。
我想自定义它以返回包含错误信息的 JSON 或 XML 响应。

错误是 MultiJson::DecodeError 我知道我可以用 rescue_from 捕获异常在 ApplicationController 中...但它似乎不起作用。
这就像错误发生在正常请求流的之外

这是我的代码(一旦我开始工作,我将使用更多数据扩展错误消息):

class ApplicationController < ActionController::Base
#protect_from_forgery
rescue_from MultiJson::DecodeError do |exception|
@response = { :error => "the request body was not acceptable" }
respond_to do |format|
format.html { redirect_to :root, notice: "invalid params" }
format.xml { render :xml => @response, status: 400 }
format.json { render :json => @response, status: 400 }
end
end
end

这是回溯:

MultiJson::DecodeError (795: unexpected token at '{
"json_with_missing_comma" : {
"foo" : "qqqqqqqqqqqqqqq"
"bar" : "aaaaaaaaaaaaaa"
}}'):
json (1.7.6) lib/json/common.rb:155:in `parse'
json (1.7.6) lib/json/common.rb:155:in `parse'
multi_json (1.5.0) lib/multi_json/adapters/json_common.rb:7:in `load'
multi_json (1.5.0) lib/multi_json.rb:96:in `load'
activesupport (3.2.11) lib/active_support/json/decoding.rb:15:in `decode'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.4) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.4) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1562301902235545482__call__1964551201027599208__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.4) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.4) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.4) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.4) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.0) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.0) lib/thin/connection.rb:79:in `catch'
thin (1.5.0) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.0) lib/thin/connection.rb:54:in `process'
thin (1.5.0) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run'
thin (1.5.0) lib/thin/backends/base.rb:63:in `start'
thin (1.5.0) lib/thin/server.rb:159:in `start'
rack (1.4.4) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.4) lib/rack/server.rb:268:in `start'
railties (3.2.11) lib/rails/commands/server.rb:70:in `start'
railties (3.2.11) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.11) lib/rails/commands.rb:50:in `tap'
railties (3.2.11) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

最佳答案

gem :https://github.com/kares/request_exception_handler连同以下几行对我有用。

在应用程序 Controller 中:“ rescue_from 'REXML::ParseException' 执行 |exception| render :text => "Bad Request: XML parse exception", :status => 422 结束

rescue_from 'MultiJson::DecodeError' 做 |exception| render :text => "Bad Request: JSON parse exception", :status => 422 结尾"

关于ruby-on-rails - Rails 3.2,处理损坏的请求体(MultiJson::DecodeError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com