#53
Jul 04, 2007

Handling Exceptions

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.
Tags: controllers
Download (29.3 MB, 8:32)
alternative download for iPod & Apple TV (14.1 MB, 8:32)
# application.rb
def local_request?
  false
end

def rescue_action_in_public(exception)
  case exception
  when ActiveRecord::RecordNotFound
    render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
  else
    super
  end
end

18 comments

xajler Jul 04, 2007 at 00:29

Thanks, very good cast like all before. Will you make one about authorization and authentication?


Nico Orellana Jul 04, 2007 at 00:30

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.


chineseGuy Jul 04, 2007 at 00:33

thanks


Mahmoud MHIRI Jul 04, 2007 at 01:24

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.


Mahmoud MHIRI Jul 04, 2007 at 01:26

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:// :)


Jim Jul 04, 2007 at 01:45

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!


Simon Jul 04, 2007 at 02:04

Yes you can. The full exception can always be found in the variable $!


Lisa Jul 04, 2007 at 03:29

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/


Adam Jul 04, 2007 at 03:50

@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>


Ryan Bates Jul 04, 2007 at 07:31

@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.


Trevor Turk Jul 04, 2007 at 10:44

Hooray for Railscasts! :)


wak karto Jul 04, 2007 at 17:32

Thanks Ryan, anybody knew how to upload file using attachment_fu plugin.Ryan, will you make one?


Chris Olsen Jul 06, 2007 at 12:00

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


vlad Jul 08, 2007 at 06:40

anyone know how to show the process name as the tab title in iterm like in the video?


vlad Jul 08, 2007 at 07:43

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.


Ryan Bates Jul 08, 2007 at 08:18

@vlad, I just edited the title in the session information panel (command-i).


vlad Jul 08, 2007 at 08:48

ryan, i meant automatically.


kino May 23, 2008 at 01:54

The intelligible objects in space and time are by their very nature contradictory.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player