From cf8fe08000143c17bdd8203fbc246df8fdd7475b Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 2 May 2023 16:05:58 +0200 Subject: [PATCH] Get rid of TrilogyAdapter's special errors Their value seem very limited, and I fear they cause users to rescue these specifc errors rather than the general `ConnectionFailed` one. --- .../connection_adapters/trilogy/errors.rb | 49 -------------- .../lost_connection_exception_translator.rb | 64 ------------------- .../connection_adapters/trilogy_adapter.rb | 19 +++++- 3 files changed, 16 insertions(+), 116 deletions(-) delete mode 100644 activerecord/lib/active_record/connection_adapters/trilogy/errors.rb delete mode 100644 activerecord/lib/active_record/connection_adapters/trilogy/lost_connection_exception_translator.rb diff --git a/activerecord/lib/active_record/connection_adapters/trilogy/errors.rb b/activerecord/lib/active_record/connection_adapters/trilogy/errors.rb deleted file mode 100644 index dfd9ca3dcc677..0000000000000 --- a/activerecord/lib/active_record/connection_adapters/trilogy/errors.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -module ActiveRecord - module ConnectionAdapters - module Trilogy - module Errors - # ServerShutdown will be raised when the database server was shutdown. - class ServerShutdown < ActiveRecord::ConnectionFailed - end - - # ServerLost will be raised when the database connection was lost. - class ServerLost < ActiveRecord::ConnectionFailed - end - - # ServerGone will be raised when the database connection is gone. - class ServerGone < ActiveRecord::ConnectionFailed - end - - # BrokenPipe will be raised when a system process connection fails. - class BrokenPipe < ActiveRecord::ConnectionFailed - end - - # SocketError will be raised when Ruby encounters a network error. - class SocketError < ActiveRecord::ConnectionFailed - end - - # ConnectionResetByPeer will be raised when a network connection is closed - # outside the sytstem process. - class ConnectionResetByPeer < ActiveRecord::ConnectionFailed - end - - # ClosedConnection will be raised when the Trilogy encounters a closed - # connection. - class ClosedConnection < ActiveRecord::ConnectionFailed - end - - # InvalidSequenceId will be raised when Trilogy ecounters an invalid sequence - # id. - class InvalidSequenceId < ActiveRecord::ConnectionFailed - end - - # UnexpectedPacket will be raised when Trilogy ecounters an unexpected - # response packet. - class UnexpectedPacket < ActiveRecord::ConnectionFailed - end - end - end - end -end diff --git a/activerecord/lib/active_record/connection_adapters/trilogy/lost_connection_exception_translator.rb b/activerecord/lib/active_record/connection_adapters/trilogy/lost_connection_exception_translator.rb deleted file mode 100644 index 8c41f49133943..0000000000000 --- a/activerecord/lib/active_record/connection_adapters/trilogy/lost_connection_exception_translator.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -module ActiveRecord - module ConnectionAdapters - module Trilogy - class LostConnectionExceptionTranslator - attr_reader :exception, :message, :error_number - - def initialize(exception, message, error_number) - @exception = exception - @message = message - @error_number = error_number - end - - def translate - translate_database_exception || translate_ruby_exception || translate_trilogy_exception - end - - private - ER_SERVER_SHUTDOWN = 1053 - CR_SERVER_LOST = 2013 - CR_SERVER_LOST_EXTENDED = 2055 - CR_SERVER_GONE_ERROR = 2006 - - def translate_database_exception - case error_number - when ER_SERVER_SHUTDOWN - Errors::ServerShutdown.new(message) - when CR_SERVER_LOST, CR_SERVER_LOST_EXTENDED - Errors::ServerLost.new(message) - when CR_SERVER_GONE_ERROR - Errors::ServerGone.new(message) - end - end - - def translate_ruby_exception - case exception - when Errno::EPIPE - Errors::BrokenPipe.new(message) - when SocketError, IOError - Errors::SocketError.new(message) - when ::Trilogy::ConnectionError - if message.include?("Connection reset by peer") - Errors::ConnectionResetByPeer.new(message) - end - end - end - - def translate_trilogy_exception - return unless exception.is_a?(::Trilogy::Error) - - case message - when /TRILOGY_CLOSED_CONNECTION/ - Errors::ClosedConnection.new(message) - when /TRILOGY_INVALID_SEQUENCE_ID/ - Errors::InvalidSequenceId.new(message) - when /TRILOGY_UNEXPECTED_PACKET/ - Errors::UnexpectedPacket.new(message) - end - end - end - end - end -end diff --git a/activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb b/activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb index a07ee73f21722..39e88e82f6324 100644 --- a/activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb @@ -6,8 +6,6 @@ require "trilogy" require "active_record/connection_adapters/trilogy/database_statements" -require "active_record/connection_adapters/trilogy/lost_connection_exception_translator" -require "active_record/connection_adapters/trilogy/errors" module ActiveRecord module ConnectionHandling # :nodoc: @@ -41,6 +39,7 @@ class TrilogyAdapter < AbstractMysqlAdapter ER_BAD_DB_ERROR = 1049 ER_DBACCESS_DENIED_ERROR = 1044 ER_ACCESS_DENIED_ERROR = 1045 + ER_SERVER_SHUTDOWN = 1053 ADAPTER_NAME = "Trilogy" @@ -259,7 +258,21 @@ def get_full_version def translate_exception(exception, message:, sql:, binds:) error_code = exception.error_code if exception.respond_to?(:error_code) - Trilogy::LostConnectionExceptionTranslator.new(exception, message, error_code).translate || super + case error_code + when ER_SERVER_SHUTDOWN + return ConnectionFailed.new(message) + end + + case exception + when Errno::EPIPE, SocketError, IOError + return ConnectionFailed.new(message) + when ::Trilogy::Error + if /Connection reset by peer|TRILOGY_CLOSED_CONNECTION|TRILOGY_INVALID_SEQUENCE_ID|TRILOGY_UNEXPECTED_PACKET/.match?(exception.message) + return ConnectionFailed.new(message) + end + end + + super end def default_prepared_statements