# Mysql2 throws errors on invalid dates. Need suggestion for catching

**URL:** <https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954>\
**Category:** Rails\
**Created:** [September 14, 2010, 9:34am UTC](https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954 "2010-09-14T09:34:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rambor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rambor](https://www.ruby-forum.com/u/rambor)\
**Post date:** [September 14, 2010, 9:34am UTC](https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954/1 "2010-09-14T09:34:11Z")

</div>

A dataset I’m working with has bad dates in it (2010-00-09 for  
example). This causes Mysql2 to throw an error at the adapter level.  
How am I supposed to catch it so I can handle it correctly? I don’t  
think it fails in ActiveRecord land, so I’m not sure how to handle it.

I figured since Rails 3 uses it by default, someone here would have  
some experience.

I know I could fix the data, but I’m not sure when/if another batch  
will come in like that and I want to make sure my app is bulletproof.

Thanks

---

<div class="post-metadata">

**Author:** ![rambor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rambor](https://www.ruby-forum.com/u/rambor)\
**Post date:** [September 14, 2010, 10:13am UTC](https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954/2 "2010-09-14T10:13:24Z")

</div>

On 14 sep, 07:11, Robert R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know I could fix the data, but I’m not sure when/if another batch  
> will come in like that and I want to make sure my app is bulletproof.

Seems like you should fix the data, and add validations to ensure that  
bad data doesn’t get into the database again.

Cheers,  
Wincent

---

<div class="post-metadata">

**Author:** ![rambor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rambor](https://www.ruby-forum.com/u/rambor)\
**Post date:** [September 20, 2010, 6:07am UTC](https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954/3 "2010-09-20T06:07:41Z")

</div>

I’m not in control of the data and it comes in external to my app.  
It’s an infrastructure I can’t change.

If I had control, I would do what you suggested.

---

<div class="post-metadata">

**Author:** ![rambor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rambor](https://www.ruby-forum.com/u/rambor)\
**Post date:** [September 20, 2010, 7:10am UTC](https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954/4 "2010-09-20T07:10:54Z")

</div>

make a before save method in the model and user a regular expression to  
translate the date i do something similar changing oracle to mysql.

---

<div class="post-metadata">

**Author:** ![rambor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rambor](https://www.ruby-forum.com/u/rambor)\
**Post date:** [September 20, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/mysql2-throws-errors-on-invalid-dates-need-suggestion-for-catching/194954/5 "2010-09-20T17:49:34Z")

</div>

Please quote when replying. It’s very hard to follow the discussion  
otherwise.

Robert R. wrote:

> I’m not in control of the data and it comes in external to my app.  
> It’s an infrastructure I can’t change.
> 
> If I had control, I would do what you suggested.

Well, if you’re getting fed bad data, it’s not reasonable for your  
end-users to expect good results. Garbage in, garbage out.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
