我开发了一个在json中回答的rest webservice。 但是当抛出一些(不是全部)异常时,我无法捕获它们,并且rails会渲染一个htlm,这对我的所有客户端来说都不是一个有效的响应。 如果在此catch all调用的方法中引发异常,则会出现问题:
rescue_from Exception, with: :render_unkown_error
我不得不承认,在最后一次机会中提出一个例外非常非常关键, 但我想知道:
有没有办法在发生此类错误时完全解除所有rails响应,而不是返回包含堆栈跟踪的html页面?
编辑:
实际上,这个问题也出现在我的控制器中的未定义属性中:
ActionController::RoutingError (undefined local variable or method `truc' for V2::Model3dsController:Class):
app/controllers/v2/model3ds_controller.rb:8:in `<class:Model3dsController>'
app/controllers/v2/model3ds_controller.rb:4:in `<module:V2>'
app/controllers/v2/model3ds_controller.rb:3:in `<top (required)>'
即使我在我的基本控制器中有以下救援:
rescue_from ActionController::RoutingError, with: :render_routing_error
EDIT2:
如果我的webservice的用户向我的webservice发送了格式错误的参数,我就会遇到同样的问题。 由于执行流程没有通过我的代码,因此不处理异常:
Started POST "/users" for XXX.XXX.XXX.XXX at 2013-05-07 11:11:27 +0000
Error occurred while parsing request parameters.
Contents:
MultiJson::LoadError (795: unexpected token at 'url=http%3A%2F%2Ftest.test.com%2Fusers'):
json (1.7.7) lib/json/common.rb:155:in `parse'
json (1.7.7) lib/json/common.rb:155:in `parse'
multi_json (1.7.2) lib/multi_json/adapters/json_common.rb:16:in `load'
multi_json (1.7.2) lib/multi_json/adapter.rb:19:in `load'
multi_json (1.7.2) lib/multi_json.rb:120:in `load'
activesupport (3.2.13) lib/active_support/json/decoding.rb:15:in `decode'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__3866502263790514870__call__1366634820855087578__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `catch'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
eventmachine (1.0.3) lib/eventmachine.rb:1037:in `call'
eventmachine (1.0.3) lib/eventmachine.rb:1037:in `block in spawn_threadpool'
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0@websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms)
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0@websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0@websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.7ms)
答案 0 :(得分:1)
你想要的是exceptions_app
。
Rails exceptions_app
基本上是rails的配置选项,默认为PublicExceptions
中的中间件ActionDispatch
。
这几乎是如果没有提供,则exception_app默认为:
<强> PublicExceptions 强>
这是处理显示公共500.html和400.html
的public_exceptions中间件显示例外
这是显示开发/生产异常的中间件的一部分
<强>默认值强>
这是初始化应用程序时默认的内容
https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L391-L393
您可以将其换成应用程序或将其路由到您自己的rails应用程序,如下所示:
<强>配置/ application.rb中强>
config.exceptions_app = self.routes
然后在您的路线中,您将匹配此类路线
<强>的routes.rb 强>
match "/500" => "errors#exception"
match "/404" => "errors#not_found"
因此,您将拥有一个ErrorsController,它将具有这些操作并处理对它们的响应。
<强> errors_controller.rb 强>
class ErrorsController < ApiController
def exception
render json: {error: "Internal Error"}, status: 500
end
def not_found
render json: {error: "Not Found"}, status: 404
end
end