# AR - custom error handling

**URL:** <https://www.ruby-forum.com/t/ar-custom-error-handling/170543>\
**Category:** Rails\
**Created:** [June 18, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/ar-custom-error-handling/170543 "2009-06-18T18:22:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 18, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/ar-custom-error-handling/170543/1 "2009-06-18T18:22:08Z")

</div>

I have this code in a controller:

```
respond_to do |format|
  if @currency.save
    flash[:notice] = 'Currency was successfully created.'
    format.html { redirect_to(@currency) }
    format.xml { render :xml => @currency,
      :status => :created, :location => @currency }
  else
    format.html { render :action => "new" }
    format.xml { render :xml => @currency.errors,
      :status => :unprocessable_entity }
  end

```

I have this code in the associated AR model:

# save

# 

# Override AR save method to catch DBMS specific errors

# such as uniqueness constraint violations

def save  
super  
rescue ActiveRecord::StatementInvalid =\> this\_exception  
errors.add\_to\_base(hll\_ar\_exception(this\_exception))  
end

When I attempt to add a record in violation of a uniqueness constraint  
then hll\_ar\_exception catches the dbms error and returns. If the error  
is something else then it raises the original error again.

I expect that if the custom error handler returns a value to the  
instance save method then a Rails error is raised with the provided text  
and displayed in the view. However, this does not happen. Instead I  
receive a record added successfully notice, even though the new row is  
not actually inserted.

Further, if I make this one change to the model code:

# save

# 

# Override AR save method to catch DBMS specific errors

# such as uniqueness constraint violations

def save  
super  
rescue ActiveRecord::StatementInvalid =\> this\_exception  
errors.add\_to\_base(hll\_ar\_exception(this\_exception))  
puts hll\_ar\_exception(this\_exception) ##### \<=== add this line  
end

Then I do get the error message returned from hll\_ar\_exception displayed  
in the view as an error with the appropriate field highlighted.

Can somebody explain to me what is going on?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 18, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/ar-custom-error-handling/170543/2 "2009-06-18T18:45:43Z")

</div>

On Jun 18, 5:22 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> When I attempt to add a record in violation of a uniqueness constraint  
> then hll\_ar\_exception catches the dbms error and returns. If the error  
> is something else then it raises the original error again.

You are using the return value of save. in the case when your rescue  
block is invoked, the return value of save is what your rescue clause  
evaluates to and it would seem that errors.add\_to\_base always  
evaluates to something true. You need to return false from that block  
(which your puts statement does since puts always evaluates to nil)

Fred

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 18, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/ar-custom-error-handling/170543/3 "2009-06-18T18:59:46Z")

</div>

Frederick C. wrote:

> You are using the return value of save. in the case when your rescue  
> block is invoked, the return value of save is what your rescue clause  
> evaluates to and it would seem that errors.add\_to\_base always  
> evaluates to something true. You need to return false from that block  
> (which your puts statement does since puts always evaluates to nil)
> 
> Fred

Thanks.

Regards,
