# Methods that return a hash or an exception?

**URL:** <https://www.ruby-forum.com/t/methods-that-return-a-hash-or-an-exception/57052>\
**Category:** Ruby\
**Created:** [April 6, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/methods-that-return-a-hash-or-an-exception/57052 "2006-04-06T21:07:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [April 6, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/methods-that-return-a-hash-or-an-exception/57052/1 "2006-04-06T21:07:00Z")

</div>

Hi everyone,

I’m not quite sure how to handle this type of thing. I have a method  
that searches for a user in our LDAP by way of a Person model’s  
username attribute:

@ldap\_person = @person.get\_ldap\_user

This method, noce done with the LDAP wrangling, returns the user entry  
(an LDAP::Entry object) as a hash:

return conn.search2(UnifiedLDAP::PEOPLE\_DN, LDAP::LDAP\_SCOPE\_SUBTREE,  
filter, self.ldap\_attributes)[0].to\_hash

This works, and, of course, there SHOULD be an LDAP entry  
corresponding to that Person, but if there isn’t or LDAP is  
unreachable, I want to report that. Am I doing the wrong thing in  
returning a hash? Where should I be catching exceptions? How should  
I be then be dealing with them?

This is part of a Rails app, but I don’t think that part is important,  
so I’m sticking to this list. When I pass @ldap\_person to the view,  
though, and @ldap\_person is an exception, obviously, this doesn’t  
work.

Thank you!

Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [April 6, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/methods-that-return-a-hash-or-an-exception/57052/2 "2006-04-06T21:40:49Z")

</div>

Sean H. wrote:

> so I’m sticking to this list. When I pass @ldap\_person to the view,  
> though, and @ldap\_person is an exception, obviously, this doesn’t  
> work.

One idea off the top of my head would be to catch the exception in  
#get\_ldap\_user and have the rescue clause return nil. Then, when you  
pass @ldap\_person to your view, you could have something like: \<%  
@ldap\_person || “” %\> in your rhtml file, so that if @ldap\_person is  
nil, it just prints an empty string.

HTH,

Jamey

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [April 6, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/methods-that-return-a-hash-or-an-exception/57052/3 "2006-04-06T22:17:29Z")

</div>

Ok, yeah, that worked. Thanks! There’s a little more code in the  
view and controller than I’d like, but I’ll be able to refactor that  
as I get better at Ruby. 🙂

What I have now (the Person model just returns nil if there was an  
exception):

if !(@ldap\_person = @person.get\_ldap\_user)  
flash[:error] = “User does not exist in LDAP or LDAP unreachable”  
end

View:

\<% if flash[:error] %\>  
\<%= flash[:error] %\>  
\<% flash[:error] = nil %\>  
\<% else %\>  
…everything else for a successful user find…  
\<% end %\>

So, yeah, not the best, but it definitely gets me to a place where I  
can show this to the client and work on fleshing out error handling a  
bit more.

Thank you!

Sean
