1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Raise specific exception on Mysql2::Error::TimeoutError

This commit is contained in:
Kir Shatrov 2019-07-16 20:08:50 +01:00
parent da4e18c5c2
commit 080939c987
3 changed files with 23 additions and 1 deletions

View file

@ -619,7 +619,11 @@ module ActiveRecord
when ER_QUERY_INTERRUPTED
QueryCanceled.new(message, sql: sql, binds: binds)
else
super
if exception.is_a?(Mysql2::Error::TimeoutError)
ActiveRecord::AdapterTimeout.new(message, sql: sql, binds: binds)
else
super
end
end
end

View file

@ -365,6 +365,10 @@ module ActiveRecord
class QueryCanceled < StatementInvalid
end
# AdapterTimeout will be raised when database clients times out while waiting from the server
class AdapterTimeout < StatementInvalid
end
# UnknownAttributeReference is raised when an unknown and potentially unsafe
# value is passed to a query method when allow_unsafe_raw_sql is set to
# :disabled. For example, passing a non column name value to a relation's

View file

@ -225,6 +225,20 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase
end
end
def test_read_timeout_exception
ActiveRecord::Base.establish_connection(
ActiveRecord::Base.configurations[:arunit].merge("read_timeout" => 1)
)
error = assert_raises(ActiveRecord::AdapterTimeout) do
ActiveRecord::Base.connection.execute("SELECT SLEEP(2)")
end
assert_equal Mysql2::Error::TimeoutError, error.cause.class
ensure
ActiveRecord::Base.establish_connection :arunit
end
private
def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block)
super(@conn, "ex", definition, &block)