# Global Exception handler

**URL:** <https://www.ruby-forum.com/t/global-exception-handler/112227>\
**Category:** Rails\
**Created:** [August 16, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/global-exception-handler/112227 "2007-08-16T16:58:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_De\_Berdt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@Peter\_De\_Berdt](https://www.ruby-forum.com/u/Peter_De_Berdt)\
**Post date:** [August 16, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/global-exception-handler/112227/1 "2007-08-16T16:58:22Z")

</div>

Hi

Is there a way in Ruby or Rails to have a global exception handler to  
rescue from a StaleObject exception? I know about  
rescue\_action\_in\_public, but I don’t know how to get the object the  
exception occurred on.

For example, while updating a record identified as @my\_record, a  
StaleObject exception is thrown. I would like to implement a global  
way of grabbing the class variable in the exception handling and  
automatically comparing all the attributes from params[:my\_record] to  
@my\_record.  
If the record has become stale on attributes that weren’t changed by  
the user (i.e. user 1 changed field “title”, while user 2 only  
changed field “summary”), although the record has become stale,  
updating the record with the updated fields on the newer version of  
the record doesn’t need additional user intervention. If the  
attributes changed by both users are identical, it would be handled  
differently.

I know I’m simplifying things a bit, but I think you pretty much get  
the picture.

I just want to avoid having to repeat myself all the time:  
begin  
…  
rescue ActiveRecord::StaleObjectError  
if automatically\_handlestaleobject(@my\_record)  
redirect\_to …  
else  
render “a\_layout\_or\_even\_rjs\_to\_let\_the\_user\_handle\_it”  
end  
end

Best regards

Peter De Berdt
