views:

66

answers:

2

If there's an exception in a Rails application, one gets an error page with the call stack, request parameters and a code excerpt.

  1. Is it possible to create a different output if the request is an XHR request?
  2. Is it possible to re-define the exception output in general? (I assume that would automatically answer the first question)
+1  A: 

You only see the page with traceback in development mode, while in production mode you see a standard error page (located in public/500.html) which just says an error occurred.

This is meant for security reasons, and it's not, of course, limited to rails: all web application frameworks do the same, as the backtrace can disclose sensitive information (it sometimes happen that you see an error message on a web app displaying the db connection string, or some password, or the like; well, you don't want this to happen).

In development mode, on XHR calls, you still receive the backtrace (I use firebug to debug my apps, so I just copy it and paste somewhere).

In production mode you can handle XHR errors from within the ajax call, by explicitly set a function to be executed on error by setting the :failure param of functions like remote_function.

giorgian
+1  A: 

You could try overriding rescue_action in your action controller.

def rescue_action(exception)
  if request.xhr?
    custom_xhr_error_handling_for(exception)
  else
    super
  end
end

The more traditional way is to use rescue_from Exception, :custom_xhr_error_handling_for but that removes your ability to let the default code do the dirty work if it later turns out it was not an xhr response.

cwninja