Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4615

How can I capture mariadb "Duplicate entry" exceptions in Ruby?

$
0
0

I'm using the mysql2 gem to access MariaDB from Ruby to INSERT thousands of records. These sometimes cause "Duplicate entry" errors, due to violating a UNIQUE key I've assigned, specifically for the purpose of avoiding duplicates.

Currently, when MariaDB sees a duplicate on a UNIQUE key, it throws up its hands and quits, causing the Ruby script to quit, as well.

I would like to be able to log and bypass this innocuous error and continue processing non-unique data.

I have found an StandardError subclass for the Mysql2 gem, but it only specifies a limited set of what appear to be relatively important system errors, such as ER_ABORTING_CONNECTION and CR_SERVER_HANDSHAKE_ERR, with no way of specifically handling SQL errors.

I'm currently rescueing all MariaDB errors, but I would rather just handle ones (like "Duplicate entry") that I know I can handle, and let anything more critical cause a program crash.

Any ideas on the best way to manage this? TIA!


Viewing all articles
Browse latest Browse all 4615

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>