I have my own exception handler:
module Frog
module Errors
class NotFound < FrogError
attr_accessor :exception
def initialize (exception)
self.exception = exception
end
def as_json
{
:error => {
:message => "Object not found"
}
}
end
def status_code
404
end
end
end
end
In application_controller.rb this exception is handled by
rescue_from Frog::Errors::FrogError, :with => :render_frog_error开发者_JS百科
and
def render_frog_error(exception)
access_control_headers!
render :json => exception, :status => exception.status_code
end
In my project i have BSON::InvalidObjectId and Mongoid::Errors::DocumentNotFound exceptions. I want to generate this exceptions by myself. I try this way:
rescue_from BSON::InvalidObjectId do
|ex| raise Frog::Errors::NotFound.new(ex)
end
but it's not working. How can I reraise BSON adn Mongoid exception to mine?
I found this solution:
rescue_from BSON::InvalidObjectId, :with => :proxy_exception
rescue_from Mongoid::Errors::DocumentNotFound, :with => :proxy_exception
def proxy_exception(exception)
exception = Frog::Errors::NotFound.new(exception)
render_frog_error(exception)
end
精彩评论