iSabanin / rescue_from_templates_exceptions
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
tree 02aee664541fad2056fdf3e771d0d28eb57c7f68
parent 1f977fbf97362427db4525cfc3a2f54f0a4c2b0c
| name | age | message | |
|---|---|---|---|
| |
.gitignore | ||
| |
MIT-LICENSE | ||
| |
README.textile | ||
| |
Rakefile | ||
| |
init.rb | ||
| |
lib/ |
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

