0
默認情況下,Rails爲404 Resource Not Found
和500 Internal Server Error
提供有用的HTML頁面。但是對於我的JSON API,我想提供JSON格式的回覆。針對JSON請求的Rails 3默認異常+ 404處理
如何爲JSON請求設置默認的異常處理?在routes.rb
NOT_FOUND_EXC = [ActionController::RoutingError,
AbstractController::ActionNotFound,
ActiveRecord::RecordNotFound]
class BaseapiController < ActionController::Base
respond_to :json
rescue_from StandardError, with: :show_json_error
def resource_not_found
url = request.fullpath
respond_with(url), status: :not_found)
end
private
def show_json_errors(exc)
case exc
when *NOT_FOUND_EXC
return resource_not_found
else
logger.error exc.message
logger.error exc.backtrace.join("\n")
respond_with(exc), status: :internal_server_error)
end
end
end
而且我已經定義是::