# How to catch Oracle errors in a rake task

**URL:** <https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326>\
**Category:** Rails\
**Created:** [February 11, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326 "2010-02-11T19:50:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [February 11, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326/1 "2010-02-11T19:50:43Z")

</div>

How can I trap the following Oracle error in a rake task? I can trap  
most Oracle errors in the website with “@phone.errors.count \> 0”, but  
in the rake task the task stops before a check can be done.

The rake task used to work before the Oracle DB administrator decided  
to put in foreign key, integrity constraints in Oracle.

rake aborted!  
OCIError: ORA-00001: unique constraint (OWNER.PHONE\_NUMBERS\_PK)  
violated: INSERT INTO phone\_numbers

---

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [February 11, 2010, 8:24pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326/2 "2010-02-11T20:24:41Z")

</div>

thanks for the response.  
I have “validates\_uniqueness\_of :phone\_number” in the model, and it  
worked fine until the DBA got involved.  
Now for this error, Oracle just causes the rake task to abort rather  
than returning an error.

---

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [February 11, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326/3 "2010-02-11T20:02:26Z")

</div>

On Thu, Feb 11, 2010 at 12:48 PM, sultan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How can I trap the following Oracle error in a rake task?

Ruby has exception handling:

[http://ruby-doc.org/docs/ProgrammingRuby/html/tut\_exceptions.html](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html)

> I can trap  
> most Oracle errors in the website with “@phone.errors.count \> 0”, but  
> in the rake task the task stops before a check can be done.
> 
> The rake task used to work before the Oracle DB administrator decided  
> to put in foreign key, integrity constraints in Oracle.

You can do a find to see if you are about to cause an exceptions  
before you cause it.

> rake aborted!  
> OCIError: ORA-00001: unique constraint (OWNER.PHONE\_NUMBERS\_PK)  
> violated: INSERT INTO phone\_numbers

You need to duplicate this constraint in the model using  
validates\_uniqueness\_of :phone\_number.

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

---

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [February 11, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326/4 "2010-02-11T20:51:37Z")

</div>

On Thu, Feb 11, 2010 at 1:23 PM, sultan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks for the response.  
> I have “validates\_uniqueness\_of :phone\_number” in the model, and it  
> worked fine until the DBA got involved.  
> Now for this error, Oracle just causes the rake task to abort rather  
> than returning an error.

The DBA is doing exactly what she should be doing, IMHO.

Clean up your data. Stop breaking the foreign key constraints.

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

---

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [February 11, 2010, 8:59pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326/5 "2010-02-11T20:59:50Z")

</div>

The data is fine, and I am not trying to break any foreign key  
constraints.  
I am getting an error on an insert, all I need to know is how to  
capture the error before Oracle aborts the rake task.  
Can I request you to respectfully to bow out of this conversion, so  
that someone else may try to help?

---

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [February 11, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/how-to-catch-oracle-errors-in-a-rake-task/183326/6 "2010-02-11T21:12:07Z")

</div>

On Thu, Feb 11, 2010 at 1:58 PM, sultan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The data is fine, and I am not trying to break any foreign key  
> constraints.  
> I am getting an error on an insert, all I need to know is how to  
> capture the error before Oracle aborts the rake task.  
> Can I request you to respectfully to bow out of this conversion, so  
> that someone else may try to help?

Sure thing. I’m guessing lots of people are dying to assist you with  
fixing your broken DBA.

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