views:

27

answers:

1

Is there something similar to an after_filter that still runs if the action raises an exception?

I'm using an external logger (since I'm on Heroku); the response headers are filtered and logged in the after_filter. If an exception is raised, the filter doesn't run, and I don't have a log of the response header data.

If I try to hook into log_error or rescue_action_in_public, the response header won't be complete (since the actual render is called after these).

Is there another function that I can override that will be called at the equivalent time to an after_filter, but always run regardless of whether an exception is thrown?

Thanks!

A: 

You can use an around_filter and catch the exceptions. Eg

# in a controller
around_filter :catch_exceptions

def catch_exceptions
  yield
rescue ActiveRecord::RecordNotFound
  permission_denied_response # gives a stock error page
end

You can add the around_filter in the application.rb controller class or in an individual controller's class.

Larry K
That catches exceptions, but doesn't give the complete response headers, I believe. Whatever you render in the rescue isn't included . . . no?
Andrew C
If there is an exception then there was no original response or headers...I'm not sure what your q is. Maybe you want http://github.com/rails/exception_notification which emails you with a stack trace, args and more when there's an exception.
Larry K