# How to capture a SOAP exception from a webservice?

**URL:** <https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029>\
**Category:** Ruby\
**Created:** [August 2, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029 "2006-08-02T23:42:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![imaqaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@imaqaguy](https://www.ruby-forum.com/u/imaqaguy)\
**Post date:** [August 2, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029/1 "2006-08-02T23:42:35Z")

</div>

Iâ??m trying to catch a soap exception from a web service, but Iâ??m a n00b  
and am having a hard time figuring it out.

Iâ??m using ruby 1.8.4 (win32)–if it matters.

Iâ??ve got the start of my program created and can receive successful soap  
objects back from the webservice. So thatâ??s all working good.

Next, I want to capture SOAP exception errors. For example, Iâ??ll get a  
message saying something does not exist or is not found. This is the  
same message that is in the return XML â??faultstringâ? tag.

Do I have to find a way to parse the returned XML, or is there an easy  
way to grab the soap exception I’m looking for?

Thanks for the help. BTW, I tried to use the forum search (I always try  
that first), but it’s down for maintenance or something like that.

Thanks all,

-nate

---

<div class="post-metadata">

**Author:** ![imaqaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@imaqaguy](https://www.ruby-forum.com/u/imaqaguy)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029/2 "2006-08-03T14:23:23Z")

</div>

begin  
@query = @soap.apiFunction(  
{‘Key’=\>‘Value’,‘Key2’=\>Value2’}  
)  
#rescue :🧼:FaultError =\> e  
rescue ::XSD::ValueSpaceError =\> e  
assert\_equal(“{[http://www.foo.com/api](http://www.foo.com/api)}FooBarEnum: cannot accept  
‘foo’”,e.to\_s)  
end

Hope that helps…  
-Chris

---

<div class="post-metadata">

**Author:** ![imaqaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@imaqaguy](https://www.ruby-forum.com/u/imaqaguy)\
**Post date:** [August 3, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029/3 "2006-08-03T19:30:04Z")

</div>

Hmmmâ?¦

**If** I understand what you were trying to tell me, I **think** I  
adapted your example to my code and came up with this. BTW, â??paramâ? is  
an int Iâ??m passing to the method.

* * *

## if results = soap.FindPatientById( param ) then puts results.first\_name puts results.last\_name else begin rescue :🧼:FaultError =\> e # rescue ::XSD::ValueSpaceError =\> e puts e.to\_s end end

I had something pretty similar to this already. â?¦but itâ??s not  
working.

The way Iâ??m doing it, I donâ??t think I needed the â??assert equalâ?  
statementâ??right? The most confusing parts to me are the â??rescueâ? lines.  
Specifically, which one to use (I went with SOAP, but tried both) and  
how you knew to put â??FaultErrorâ? there.

Like I said, Iâ??m a total n00b to ruby. I havenâ??t even tried programming  
anything since my last C class about 6 years ago, so please be patient  
if Iâ??m missing something obvious.

= )

Thanks again,

-nate

Chris McMahon wrote:

> begin  
> @query = @soap.apiFunction(  
> {‘Key’=\>‘Value’,‘Key2’=\>Value2’}  
> )  
> #rescue :🧼:FaultError =\> e  
> rescue ::XSD::ValueSpaceError =\> e  
> assert\_equal(“{[http://www.foo.com/api](http://www.foo.com/api)}FooBarEnum: cannot accept  
> ‘foo’”,e.to\_s)  
> end
> 
> Hope that helps…  
> -Chris

---

<div class="post-metadata">

**Author:** ![imaqaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@imaqaguy](https://www.ruby-forum.com/u/imaqaguy)\
**Post date:** [August 4, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029/4 "2006-08-04T18:51:43Z")

</div>

On 8/3/06, Nate I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts e.to_s
> end
> 
> ```
> 
> ## end
> 
> I had something pretty similar to this already. ?but it’s not  
> working.

From what I understand, I believe your method call has to be inside  
the “begin…rescue” block, or “rescue” won’t be able to see the error  
when it happens.

In other words, something like this:

begin  
if ( results = soap.FindPatientById( param ) )  
puts results.last\_name  
end  
rescue :🧼:FaultError =\> e  
puts e.to\_s  
end

---

<div class="post-metadata">

**Author:** ![imaqaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@imaqaguy](https://www.ruby-forum.com/u/imaqaguy)\
**Post date:** [August 5, 2006, 1:32am UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029/5 "2006-08-05T01:32:58Z")

</div>

Excellent; thanks!

Bira wrote:

> On 8/3/06, Nate I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > puts e.to_s
> > end
> > 
> > ```
> > 
> > ## end
> > 
> > I had something pretty similar to this already. ï¿½but it’s not  
> > working.
> 
> From what I understand, I believe your method call has to be inside  
> the “begin…rescue” block, or “rescue” won’t be able to see the error  
> when it happens.
> 
> In other words, something like this:
> 
> begin  
> if ( results = soap.FindPatientById( param ) )  
> puts results.last\_name  
> end  
> rescue :🧼:FaultError =\> e  
> puts e.to\_s  
> end

---

<div class="post-metadata">

**Author:** ![imaqaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ec9cab/32.png) [@imaqaguy](https://www.ruby-forum.com/u/imaqaguy)\
**Post date:** [August 4, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-capture-a-soap-exception-from-a-webservice/101029/6 "2006-08-04T16:42:11Z")

</div>

Hello.

= )

I know I’m close on this one–I can feel it. Can anyone help? I’d  
appreciate it a lot.

Thanks,

-nate

Nate I. wrote:

> Hmmmâ?¦
> 
> **If** I understand what you were trying to tell me, I **think** I  
> adapted your example to my code and came up with this. BTW, â??paramâ? is  
> an int Iâ??m passing to the method.
> 
> * * *
> 
> if results = soap.FindPatientById( param )  
> then puts results.first\_name  
> puts results.last\_name  
> else  
> begin  
> rescue :🧼:FaultError =\> e
> 
> # rescue ::XSD::ValueSpaceError =\> e
> 
> ## puts e.to\_s end end
> 
> I had something pretty similar to this already. â?¦but itâ??s not  
> working.
> 
> The way Iâ??m doing it, I donâ??t think I needed the â??assert equalâ?  
> statementâ??right? The most confusing parts to me are the â??rescueâ? lines.  
> Specifically, which one to use (I went with SOAP, but tried both) and  
> how you knew to put â??FaultErrorâ? there.
> 
> Like I said, Iâ??m a total n00b to ruby. I havenâ??t even tried programming  
> anything since my last C class about 6 years ago, so please be patient  
> if Iâ??m missing something obvious.
> 
> = )
> 
> Thanks again,
> 
> -nate
> 
> Chris McMahon wrote:
> 
> > begin  
> > @query = @soap.apiFunction(  
> > {‘Key’=\>‘Value’,‘Key2’=\>Value2’}  
> > )  
> > #rescue :🧼:FaultError =\> e  
> > rescue ::XSD::ValueSpaceError =\> e  
> > assert\_equal(“{[http://www.foo.com/api](http://www.foo.com/api)}FooBarEnum: cannot accept  
> > ‘foo’”,e.to\_s)  
> > end
> > 
> > Hope that helps…  
> > -Chris
