Disable all html exception rendering in rails
我开发了一个在json中回答的rest webservice。
但是当抛出一些(不是全部)异常时,我无法捕获它们,并且rails会渲染一个htlm,这对我的所有客户端来说都不是一个有效的响应。
如果在此catch all调用的方法中引发异常,则会出现问题:
1 | rescue_from Exception, with: :render_unkown_error |
我不得不承认,在最后一次机会中提出一个异常捕获所有非常非常关键,
但我想知道:
有没有办法在发生此类错误时完全解除所有rails响应,而不是返回包含堆栈跟踪的html页面?
编辑:
事实上,这个问题也出现在我的控制器中的未定义属性中:
1 2 3 4 | 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)>' |
即使我在我的基本控制器中有以下救援:
1 | rescue_from ActionController::RoutingError, with: :render_routing_error |
EDIT2:
如果我的webservice的用户将错误格式化的参数发送到我的webservice,我也会遇到同样的问题。
由于执行流程不通过我的代码,因此不处理异常:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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/[email protected]/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms) Rendered /home/ubuntu/.rvm/gems/[email protected]/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms) Rendered /home/ubuntu/.rvm/gems/[email protected]/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.7ms) |
你想要的是
Rails
这几乎是如果没有提供,则exception_app默认为:
PublicExceptions
这是处理显示公共500.html和400.html的public_exceptions中间件
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/public_exceptions.rb
显示例外
这是显示开发/生产异常的中间件的一部分
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中
1 | config.exceptions_app = self.routes |
然后在您的路线中,您将匹配这样的路线
的routes.rb
1 2 | match"/500" =>"errors#exception" match"/404" =>"errors#not_found" |
因此,您将拥有一个ErrorsController,它将具有这些操作并处理对它们的响应。
errors_controller.rb
1 2 3 4 5 6 7 8 9 10 | 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 |