# Manging rescues

**URL:** <https://www.ruby-forum.com/t/manging-rescues/110021>\
**Category:** Rails\
**Created:** [July 21, 2007, 11:51am UTC](https://www.ruby-forum.com/t/manging-rescues/110021 "2007-07-21T11:51:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jochen\_kaechelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jochen\_kaechelin](https://www.ruby-forum.com/u/jochen_kaechelin)\
**Post date:** [July 21, 2007, 11:51am UTC](https://www.ruby-forum.com/t/manging-rescues/110021/1 "2007-07-21T11:51:12Z")

</div>

What’s the recommended way to catch errors like this:

NoMethodError in Register#confirm  
Showing app/views/register/confirm.rhtml where line #4 raised:  
undefined method `namen’ for nil:NilClass

I get this error when someone does somethinh like:

http://…/register/confirm?email=jkdsfadslkjflksd

If I make:

remail = params[:email]  
@user = Request.find(:first, :conditions =\> [“email = ?”, remail])

the error occurs.

What’s the prefered way to react when such an error occurs?

–  
Jochen K.  
[gissmoh.de](http://gissmoh.de), [figgfrosch.de](http://figgfrosch.de), [ror-ror.de](http://ror-ror.de)

---

<div class="post-metadata">

**Author:** ![jochen\_kaechelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jochen\_kaechelin](https://www.ruby-forum.com/u/jochen_kaechelin)\
**Post date:** [July 21, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/manging-rescues/110021/2 "2007-07-21T12:33:15Z")

</div>

On 7/21/07, jochen kaechelin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

rescue\_action\_in\_public

Here’s what it does by default on 1.2.x… not very exciting:

[http://dev.rubyonrails.org/browser/branches/1-2-stable/actionpack/lib/action\_controller/rescue.rb#L50](http://dev.rubyonrails.org/browser/branches/1-2-stable/actionpack/lib/action_controller/rescue.rb#L50)

If you’re on edge you can manage this a lot easier, defining what type  
of status you want returned and template you want rendered for each

[http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action\_controller/rescue.rb#L90](http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_controller/rescue.rb#L90)

Rails will use rescue\_action\_in\_public for errors in production,  
unless the the request comes from the same IP as the server. Then,  
you get the standard dev-mode stack trace screen.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![jochen\_kaechelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jochen\_kaechelin](https://www.ruby-forum.com/u/jochen_kaechelin)\
**Post date:** [July 22, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/manging-rescues/110021/3 "2007-07-22T12:56:23Z")

</div>

jochen kaechelin wrote:

> remail = params[:email]  
> @user = Request.find(:first, :conditions =\> [“email = ?”, remail])
> 
> the error occurs.
> 
> What’s the prefered way to react when such an error occurs?

Briefly, @user might be nil, so you must guard it with “if @user” each  
time  
you use it.

I prefer to put that “if @user” line in only one place, for DRY code. I  
use  
a kind of Null Object there:

def someone  
return @user if @user  
who = OpenStruct.new(  
:login =\> ‘’,  
:namen =\> ‘’,  
:user\_id =\> nil)  
def who.delete\_data(r)  
false  
end  
return who  
end

Instead of using “@user” everywhere, I use “someone”. If nobody is  
logged  
in, “someone” provides only methods that un-logged-in people can use.  
For  
example, if logged-in users can delete data, the line  
“someone.delete\_data(a\_record)” will delete the data if a user is logged  
in,  
and it won’t do anything if nobody is logged in.

The heart of Object Oriented programming is replacing if statements with  
virtual methods that provide the correct behavior.

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![jochen\_kaechelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jochen\_kaechelin](https://www.ruby-forum.com/u/jochen_kaechelin)\
**Post date:** [July 22, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/manging-rescues/110021/4 "2007-07-22T13:00:29Z")

</div>

> > undefined method `namen’ for nil:NilClass

> rescue\_action\_in\_public

Tip: Don’t use rescue where you should just use if. The @user is just  
nil,  
per the contract of .find(), so use if @user to detect that. Don’t let  
the  
nil run around loose until something explodes, and then rescue that!

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
