# Exception handling is driving me nuts

**URL:** <https://www.ruby-forum.com/t/exception-handling-is-driving-me-nuts/150401>\
**Category:** Rails\
**Created:** [October 8, 2008, 4:19am UTC](https://www.ruby-forum.com/t/exception-handling-is-driving-me-nuts/150401 "2008-10-08T04:19:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [October 8, 2008, 4:19am UTC](https://www.ruby-forum.com/t/exception-handling-is-driving-me-nuts/150401/1 "2008-10-08T04:19:19Z")

</div>

I am utterly unable to control how exceptions are handled in my  
application and it’s driving me nuts

The idea is simple. I have a SOAP handler like this.

class RtiController \< ApplicationController

begin  
wsdl\_service\_name ‘Rti’  
web\_service\_scaffold :invoke  
web\_service\_api RtiApi

```
 before_invocation :authenticate

 include SoapMethods

 protected

 def authenticate (name, args)
    #the first two arguments are always username and password
    generic_login(args[0], args[1])
    unless logged_in?
       raise Exceptions::LoginFailedError , "LoginFailedError:

```

Invalid user name or password"  
end  
end

rescue Exception =\> e  
n = e.exception “#{e.inspect}: #{e.message}”  
n.set\_backtrace []  
raise n  
end  
end

Simple right?

If any error gets raised either in the authenticate function or any of  
the functions in the included module I want to catch them and re-raise  
a new error.

The rescue block never gets called no matter where the error happens.

Next I try this.

def rescue\_action  
do the same thing above  
end

Nope it never gets called either.

So I try this

rescue\_from Exception do |e|  
n = e.exception “#{e.inspect}: #{e.message}”  
n.set\_backtrace []  
raise n  
end

I also tried rescue\_from SomeSpecificException and that doesn’t work  
either.

I also tried putting a begin rescue block in the included module and  
that doesn’t do anything either.

Why is this so complicated? I just want a error handler for  
this controller. Is actionwebservice messing with the controller or  
what?

P.S. I am using the datanoise actionwebservice.

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [October 8, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/exception-handling-is-driving-me-nuts/150401/2 "2008-10-08T16:28:26Z")

</div>

I’m new to Rails, so I’ll preface this by saying that I might be  
wrong, but you can give it a shot. I believe you have to have a  
“begin” and “rescue” within your methods, not surrounding them as you  
illustrated above. So your authenticate method might look like this:

def authenticate (name, args)  
begin  
#the first two arguments are always username and password  
generic\_login(args[0], args[1])  
unless logged\_in?  
raise Exceptions::LoginFailedError , “LoginFailedError:  
Invalid user name or password”  
end  
rescue Exception =\> e  
n = e.exception “#{e.inspect}: #{e.message}”  
n.set\_backtrace []  
raise n  
end  
end

Since you’ll probably have multiple methods that require error  
handling, I think you can define an error handling method outside of  
each of your other methods. Instead of processing the exception  
separately within each method’s rescue function, your rescue functions  
would instead call the error handling method. See below. Not 100%  
sure this is kosher, but it’s worth a shot…and let me know if it  
works, I’m going to be building error handling into my app fairly  
soon 🙂

def method\_1  
begin  
#code  
rescue Exception =\> e  
process\_exception(e)  
end  
end

def process\_exception(e)  
#code for handling exception  
end

end

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [October 8, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/exception-handling-is-driving-me-nuts/150401/3 "2008-10-08T16:45:31Z")

</div>

On 8 Oct 2008, at 03:18, Tim U. wrote:

> ```
> web_service_scaffold :invoke
> generic_login(args[0], args[1])
> 
> ```
> 
> end  
> end
> 
> Simple right?
> 
> If any error gets raised either in the authenticate function or any of  
> the functions in the included module I want to catch them and re-raise  
> a new error.
> 
> The rescue block never gets called no matter where the error happens.

That rescue block doesn’t do what you think. The way you need to think  
about it is after the interpreter hits that begin, what code does it  
execute before it hits the rescue clause?  
It does not for example execute the authenticate method. It does  
however call the web\_service\_api method, so if that raised an  
exception then it would catch it

> rescue\_from Exception do |e|
> 
> Why is this so complicated? I just want a error handler for  
> this controller. Is actionwebservice messing with the controller or  
> what?

If you look at the actionwebservice code it is rescuing exceptions  
before you get to them (see dispatch\_web\_service\_request in  
[actionwebservice/lib/action\_web\_service/dispatcher/action\_controller\_dispatcher.rb at master · datanoise/actionwebservice · GitHub](http://github.com/datanoise/actionwebservice/tree/master/lib/action_web_service/dispatcher/action_controller_dispatcher.rb))

Fred
