views:

32

answers:

2

I want to be able to display an HTTP 500 error with an XML builder template if the request Accept type is asking for XML. By default it displays 500.html, even if the request type is not asking for an HTML response.

A: 

You can do this with a custom piece of Rack middleware. Rails 2 does exactly this in actionpack/lib/action_controller/failsafe.rb, though I'm not sure how Rails 3 is doing it. See the guide for instructions on how to do it. In your case I think you want to make your own version of that failsafe middleware, but one that checks the Accept header/url and determines the error file to use from that, then pull out the current Failsafe middleware and replace it with your version.

x1a4
A: 

Can't you do render :xml => error_message, :status => 500 ?

RobB
Not if the 500 is due to an uncaught exception, perhaps in a before_filter. Check the failsafe.rb file. Rails explicitly uses `500.html` when that middleware catches an exception, which could come from anywhere in the stack below it, including the application code. The failsafe middleware is the last resort the app has.
x1a4