Rails 4.0的自定义错误处理 [英] Custom Error Handling with Rails 4.0
问题描述
我正在使用Ruby 2.0和Rails 4.0构建Ruby on Rails api.我的应用几乎只是一个JSON API,因此如果发生错误(500、404),我想捕获该错误并返回格式正确的JSON错误消息.
I'm building a Ruby on Rails api using Ruby 2.0 and Rails 4.0. My app is almost solely a JSON API, so if an error occurs (500, 404), I want to capture that error and return a nicely formatted JSON error message.
我已经尝试过此,而且:
rescue_from ActionController::RoutingError, :with => :error_render_method
def error_render_method
puts "HANDLING ERROR"
render :json => { :errors => "Method not found." }, :status => :not_found
true
end
在我的ApplicationController中.
In my ApplicationController.
这些都不起作用(根本没有捕获到异常).我的Google搜索显示,这在3.1、3.2之间发生了很大变化,并且我找不到关于如何在Rails 4.0中执行此操作的任何好的文档.
Neither of these do the trick (the exceptions are not captured at all). My Googling shows that this changed a lot between 3.1, 3.2, and I can't find any good documentation on how to do this in Rails 4.0.
有人知道吗?
修改 这是我转到404页时的堆栈跟踪:
Edit Here's the stack trace when I go to a 404 page:
Started GET "/testing" for 127.0.0.1 at 2013-08-21 09:50:42 -0400
ActionController::RoutingError (No route matches [GET] "/testing"):
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.9ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (0.9ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.1ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (38.3ms)
我不认为我希望它走得那么远,有些东西应该抓住它并返回适当的json错误响应.
I don't think I want it to ever get this far, something should catch it and return the appropriate json error response.
推荐答案
尝试了几种变体之后,我认为这是处理API 404的最简单方法:
After trying a few variations I've settle on this as the simplest way to handle the API 404s:
# Passing request spec
describe 'making a request to an unrecognised path' do
before { host! 'api.example.com' }
it 'returns 404' do
get '/nowhere'
expect(response.status).to eq(404)
end
end
# routing
constraints subdomain: 'api' do
namespace :api, path: '', defaults: { format: 'json' } do
scope module: :v1, constraints: ApiConstraints.new(1) do
# ... actual routes omitted ...
end
match "*path", to: -> (env) { [404, {}, ['{"error": "not_found"}']] }, via: :all
end
end
这篇关于Rails 4.0的自定义错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!