# Catching XML errors in web service parameters

**URL:** <https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695>\
**Category:** Rails\
**Created:** [January 31, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695 "2008-01-31T14:01:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [January 31, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695/1 "2008-01-31T14:01:06Z")

</div>

Hi,

I’m using RoR as a web service to upload data. Unfortunately I’m often  
passed poorly formatted XML (it’s written by users, not by machine)  
and it often contains errors.

The XML parser that tries to turn the XML into parameters barfs at  
some errors (e.g. mismatched opening and closing elements) and I  
cannot see how to trap this error and report something meaningful to  
the user. At the moment they get a stacktrace or an internal error  
message.

Has anyone else come across this problem?

How can I trap these errors for myself?

Allan

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [February 1, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695/2 "2008-02-01T20:58:45Z")

</div>

bump

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [February 2, 2008, 12:09am UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695/3 "2008-02-02T00:09:52Z")

</div>

On 1/31/08, Allan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Has anyone else come across this problem?  
> Nah, you’re the first. 🙂

> How can I trap these errors for myself?

begin

# process xml

rescue  
raise “xml error: #{ $!.to\_s }” # or whatever you need to do  
end

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [February 3, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695/4 "2008-02-03T22:38:59Z")

</div>

On Feb 1, 11:08 pm, “Greg D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 1/31/08, Allan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Has anyone else come across this problem?
> 
> Nah, you’re the first. 🙂

🙂

> > How can I trap these errors for myself?
> 
> begin
> 
> # process xml
> 
> rescue  
> raise “xml error: #{ $!.to\_s }” # or whatever you need to do  
> end

Ah, I still don’t quite get it.

As far as see it, my controller is not handling the XML, the RoR  
framework is dealing with that for me, thereby populating the params  
hash and THEN my controller is invoked. My issue is that malformed  
XML is being presented to RoRand I cannot see how to trap the error.

Are you suggesting a different mechanism?

Allan

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [February 5, 2008, 10:06am UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695/5 "2008-02-05T10:06:18Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [February 5, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/catching-xml-errors-in-web-service-parameters/127695/6 "2008-02-05T12:50:41Z")

</div>

On 3 Feb 2008, at 21:38, Allan wrote:

> 🙂
> 
> As far as see it, my controller is not handling the XML, the RoR  
> framework is dealing with that for me, thereby populating the params  
> hash and THEN my controller is invoked. My issue is that malformed  
> XML is being presented to RoRand I cannot see how to trap the error.
> 
> Are you suggesting a different mechanism?

If all you want to do is display a friendly error message, then  
overriding rescue\_action\_in\_public on that controller might be what  
you want to do. Or in 2.0 you can do

class MyController \< ApplicationController  
rescue\_from SomeXmlException, :with =\> :bad\_xml

def bad\_xml(exception)  
#render some useful message to the user  
end  
end
