Module: DatabaseErrorHandler
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/database_error_handler.rb,
lib/database_error_handler/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
Instance Method Details
#handle_db_errors(retry_count: 3) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/database_error_handler.rb', line 20 def handle_db_errors(retry_count: 3) ActiveRecord::Base.transaction do yield end rescue ActiveRecord::ConnectionTimeoutError => e retry if (retry_count -= 1) > 0 raise e rescue ActiveRecord::StatementInvalid => e # mysql2 errors are wrapped into ActiveRecord::StatementInvalid case e. when /Duplicate entry/ # do nothing when /Deadlock/, /Lock wait timeout exceeded/ retry if (retry_count -= 1) > 0 else raise e end false end |
#handle_duplicate_entry(idempotent: false) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/database_error_handler.rb', line 10 def handle_duplicate_entry(idempotent: false) yield rescue Mysql2::Error, ActiveRecord::StatementInvalid => e if e. =~ /Duplicate entry/ retry if idempotent else raise e end end |