When I call an action with some invalid XML or JSON data a parse
exception gets raised from within Rails/Ruby (REXML or
ActiveSupport::JSON). The problem that I've got is how to handle these
exceptions. In my application_controller.rb I have the following for
debugging purposes:
def rescue_action_in_public(exception)
respond_to do |request|
request.all { render :text => "Uh oh" }
end
end
and the same for #rescue_action_locally. It seems that if the Content-
Type header of the request is set to application/xml or application/
json then Rails ignores my exception rescuing code and goes back a
FAILSAFE 500 error. Anyone know why this is or what I'm doing wrong?
When I call an action with some invalid XML or JSON data a parse
exception gets raised from within Rails/Ruby (REXML or
ActiveSupport::JSON). The problem that I've got is how to handle these
exceptions. In my application_controller.rb I have the following for
debugging purposes:
def rescue_action_in_public(exception)
respond_to do |request|
request.all { render :text => "Uh oh" }
end
end
and the same for #rescue_action_locally. It seems that if the Content-
Type header of the request is set to application/xml or application/
json then Rails ignores my exception rescuing code and goes back a
FAILSAFE 500 error. Anyone know why this is or what I'm doing wrong?
No conclusive answer, but a couple of suggestions that might help move
you along... First, I'd get this working in a specific controller
before moving it to application.rb. Second, 'request' refers to the
request object and might get you into trouble here. Lastly, from
looking at the respond_to source, it looks like the 'catch all' type
specifier is 'any', not 'all'. But I could be reading it wrong. You
might want to take a look.
The problem isn't in the respond_to block. Even a straight call to
render some text without using respond_to still falls back to the
FAILSAFE 500 error.
As you can see, rescue_action_locally is being called, and the content-
type of the response is set to application/json. Or does it look there
theres something my setup is missing that yours has?
I think the exception I'm dealing with happens before it has a change
to get caught by the rescue_action* methods. Maybe my backtrace will
give some insight.
I've decided to bypass ActionPack and use Rails metal instead (I was
going to use it for this eventually anyway its an API which needs to
be fast). Now I can parse the parameters myself in Rack and rescue
incoming requests that send invalid JSON/XML data nicely.