# How to handle exception in format.xml

**URL:** <https://www.ruby-forum.com/t/how-to-handle-exception-in-format-xml/133906>\
**Category:** Rails\
**Created:** [April 4, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/how-to-handle-exception-in-format-xml/133906 "2008-04-04T20:54:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [April 4, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/how-to-handle-exception-in-format-xml/133906/1 "2008-04-04T20:54:39Z")

</div>

I am developing an API for my application.  
Here is the standard show method.

def show  
@city = City.find(params[:id], :include =\> :state)

```
respond_to do |format|
  format.html
  format.xml
end

```

end

For a browser based action if the params[:id] is missing then  
ActiveRecord::RecordNotFound exception is raised which I let Rails  
handle.  
This results in public/404.html.

However in API I need to catch the exception and then I need to return  
an  
xml with the error message. What is the best way to handle that. Any  
example will be helpful

Right now even if I catch the exception then after catching the  
exception I  
need to process two different ways for html and xml. Secondly I need to  
know  
if I am processing html or xml. How do I find out if I am handing html  
or  
xml. Is there a utility method?

- Neeraj

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [April 5, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-handle-exception-in-format-xml/133906/2 "2008-04-05T18:45:12Z")

</div>

Look at the bottom of the create or update methods in the controller.  
Both have examples of handling errors that should serve as examples.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [April 8, 2008, 2:39am UTC](https://www.ruby-forum.com/t/how-to-handle-exception-in-format-xml/133906/3 "2008-04-08T02:39:41Z")

</div>

The update scenario doesn’t handle if  
@city = City.find(params[:id]) throws ActiveRecrod::RecordNotFound  
exception.

I want to return an xml stating that no record was found for the given  
id.

I can catch the exception but am not sure what is the best way to handle  
this case when I must return an xml response every single time.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [April 8, 2008, 11:50am UTC](https://www.ruby-forum.com/t/how-to-handle-exception-in-format-xml/133906/4 "2008-04-08T11:50:36Z")

</div>

On Apr 8, 2008, at 2:39 , Raj S. wrote:

> The update scenario doesn’t handle if  
> @city = City.find(params[:id]) throws ActiveRecrod::RecordNotFound  
> exception.
> 
> I want to return an xml stating that no record was found for the given  
> id.
> 
> I can catch the exception but am not sure what is the best way to  
> handle  
> this case when I must return an xml response every single time.

If that’s kind of a global rule you can easily program a catchall with  
rescue\_from in Rails 2, respond\_to is available in the handler as well.

– fxn
