在rails中禁用所有html异常渲染



我开发了一个rest webservice,它以json格式回答。但是,当抛出一些(不是全部)异常时,我无法捕获它们,并且rails呈现一个html,这对我的所有客户端都不是有效的响应。如果在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)

你想要的是exceptions_app

Rails exceptions_app基本上是Rails的一个配置选项,默认为ActionDispatch中的中间件PublicExceptions

这和exceptions_app的默认值差不多,如果没有提供:

PublicExceptions

这是public_exceptions中间件,用于显示公共的500.html和400.html

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/public_exceptions.rb

<

显示异常/strong>

这是中间件的一部分,显示开发/生产中的异常

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb L26

下面是初始化应用程序时的默认值

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

相关内容

最新更新