#53 Handling Exceptions
Jul 04, 2007 | 8 minutes | Controllers
When an exception is raised in development you get the full error along with the stack trace. In production, only a simple message is displayed. Learn why this is and how to customize the handling of exceptions.
Thanks, very good cast like all before. Will you make one about authorization and authentication?
Hello Ryan, you're making a terrific work over railscasts and the rails community, haha, is getting difficult to follow u in each episode (time!! you know! :()... cheers from Santiago de Chile.
I'm a little bit disappointed about this episode. I found it useful to know about such methods, but it wasn't what i expected! in fact, I thought that it will be sth like try.. catch.. in other languages to keep the users in the default layout.
Oh! i clicked in @Nico Orellana to visit the website.. but it seems that users are not very pleased to add each time the http:// :)
Thanks for a great railscasts. I would like to know if there is a way to mail the "rails private error page" to someone when something goes wrong, and show the user another message. It should be possible to do in the rescure_action_in_pulic, if I knew how to render the rails exception page...
Cheers!
Yes you can. The full exception can always be found in the variable $!
The exception-notifier plugin is a REALLY great way to be notified of exceptions (...) in production mode.
http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/
@Mahmoud Actually the ruby rescue system can be used in that way basically the rescue_action_in_public just gives you a chance to catch exceptions outside for all actioncontroller code
you can do a rescue block for specific errors or all error within a single action or in a begin-rescue-end style block for a specific portion of code
so
<code>
def some_action
..code..
begin
.. something that goes wrong..
rescue
.. to the rescue!..
ensure
.. Always do this!..
end
end
</code>
or
<code>
def some_other_action
.. stuff that might go wrong ..
rescue SomeSpecific::ClassOfError
.. rescue for that case ..
rescue Exception => e
.. General error of any sort ..
.. e is the class of exception ..
end
</code>
@xajler, I have done some episodes on authentication and authorization in the past. You can see them here.
http://railscasts.com/tags/9
@Mahmoud, sorry to disappoint. I considered touching on the "rescue" statement, but this is more about Ruby than Rails. Maybe it will work though.
Thanks Ryan, anybody knew how to upload file using attachment_fu plugin.Ryan, will you make one?
As always another good tutorial. I always like it when I see another video available. Although I am falling a little behind in trying to keep up :)
Thanks Ryan
anyone know how to show the process name as the tab title in iterm like in the video?
to answer my own question, i eventually found this: http://xanana.ucsc.edu/xtal/iterm_tab_customization.html
i don't think i'll be using it though, seems too hackish and does some weird things when sshing.
@vlad, I just edited the title in the session information panel (command-i).
ryan, i meant automatically.
now this works like this:
http://rails.learnhub.com/lesson/page/6392-action-controller-rescue
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
private
def record_not_found
render :text => "404 Not Found", :status => 404
end
end
Thanks for showing us how to handle this irritating error.
Just for the sake of new visitors, Rails has changed quite a bit during the last 4 years. Here's how you could do it now:
Here's some basic guide (official)
+1
Does this work well on Rails 3? I doubt it. I changed RAILS_ROOT to Rails.root. But, it doesn't work yet.
This doesn't work anymore in Rails 4.2. Instead, use rescue_from.
Example:
This episode has been updated for Rails 5 as a blog post. Handling Exceptions in Rails 5