Skip to content

isabanin/rescue_from_templates_exceptions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rescue From Templates Exceptions

You probably noticed that very useful little ActionController feature called rescue_from doesn’t work for any exceptions that were thrown in your views. This is really bad, because you never know when an exception may occur.

The reason is that all views exceptions are always of the class ActionController::TemplateError. That obscure TemplateError wrap the real exception class and therefore make it invisible for your rescue_from handlers.

With this little plugin you can make exceptions that were thrown in the views visible for rescue_from handlers and therefore catch them correctly.

Example how rescue_from not working

Let’s say you have a rescue_from handler in your ApplicationController:

ApplicationController < ActionController::Base
  
  rescue_from SomethingBadHappens, :with => :recover_from_something_bad
  
private

  def recover_from_something_bad(exception)
    Mailer.deliver_moon_message(exception.message)
    render :template => "sent_to_the_moon", :layout => 'simple'
  end
end

Now if the SomethingBadHappens is thrown in any of your controllers, it will be successfully passed to the recover_from_something_bad method and everything will be fine. But if the exception will be thrown in any of your views, your pretty recover_from_something_bad method will be completely ignored and the user will see a regular 500.html. That’s why you need to use my plugin, to make sure that the exception is always handled correctly, no matter where it came from.

Installation

Simply create a new initializer and put this code into it:

ActionController::Base.rescue_from_templates_exceptions = true

That’s all. Now the example from above will work with exceptions that come from views too. Plug and play :)

Should be a part of the Rails Core?

If you think that this functionality should be a part of the Rails itself, please let me know.

Copyright © 2009 Ilya Sabanin, released under the MIT license

About

Rails plugin that allows usage of rescue_from for views exceptions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages