【发布时间】:2013-12-24 04:52:32
【问题描述】:
我有一个 API 控制器,重要的是所有错误响应都以 JSON 格式返回。
以下设置处理操作引发的错误,但不处理 before 过滤器中引发的身份验证错误 - 它会抛出一个 401,rails 作为 text/html 发送出去。
class ApiController < ActionController::Base
rescue_from StandardError, with: :handle_errors
before_filter :restrict_access
def restrict_access
authenticate_or_request_with_http_token do |token, options|
false # Force authentication error
end
end
def handle_errors(exception)
render :json => { :errors => { :error => exception.message} }.to_json, :status => 400
end
我需要做什么来处理身份验证错误?
【问题讨论】:
标签: ruby-on-rails error-handling