Skip to content

Commit

Permalink
TrilogyAdapter: translate Trilogy::TimeoutError in AdapterTimeout
Browse files Browse the repository at this point in the history
Prior to this patch it would translate it to `InvalidStatement` which
is the catch all and is hard to work with.
  • Loading branch information
byroot committed May 22, 2023
1 parent 8c3b8bc commit 785f656
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Expand Up @@ -211,6 +211,9 @@ def get_full_version
end

def translate_exception(exception, message:, sql:, binds:)
if exception.is_a?(::Trilogy::TimeoutError) && !exception.error_code
return ActiveRecord::AdapterTimeout.new(message, sql: sql, binds: binds)
end
error_code = exception.error_code if exception.respond_to?(:error_code)

Trilogy::LostConnectionExceptionTranslator.new(exception, message, error_code).translate || super
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/adapters/trilogy/trilogy_adapter_test.rb
Expand Up @@ -322,6 +322,23 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
assert_equal 123, @conn.send(:error_number, exception)
end

test "read timeout raises ActiveRecord::AdapterTimeout" do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")

ActiveRecord::Base.establish_connection(
db_config.configuration_hash.merge("read_timeout" => 1)
)

error = assert_raises(ActiveRecord::AdapterTimeout) do
ActiveRecord::Base.connection.execute("SELECT SLEEP(2)")
end
assert_kind_of ActiveRecord::QueryAborted, error

assert_equal Trilogy::TimeoutError, error.cause.class
ensure
ActiveRecord::Base.establish_connection :arunit
end

def assert_raises_with_message(exception, message, &block)
block.call
rescue exception => error
Expand Down

0 comments on commit 785f656

Please sign in to comment.