Skip to content

Commit

Permalink
Add new exception when attempting to connect to MySQL running on loca…
Browse files Browse the repository at this point in the history
…lhost.
  • Loading branch information
ruyrocha committed Jan 22, 2024
1 parent 4816684 commit 245b035
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ def text_type?(type)
def connect
@raw_connection = self.class.new_client(@connection_parameters)
rescue ConnectionNotEstablished => ex
raise ex.set_pool(@pool)
if ex.message.match(/Can't connect to local server through socket/) && @connection_parameters[:host] == 'localhost'
raise CannotConnectThroughSocketOnLocalhost
else
raise ex.set_pool(@pool)
end
end

def reconnect
Expand Down
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ def set_pool(connection_pool)
class ConnectionTimeoutError < ConnectionNotEstablished
end

# Raised when a connection could not be obtained to the server running
# on localhost because it attempts to connect through a socket.
class CannotConnectThroughSocketOnLocalhost < ConnectionNotEstablished
def initialize(message = nil, connection_pool: nil)
message = 'The MySQL client library translates localhost address to a socket, so try to explicitly use the IP address (127.0.0.1) instead.'

super(message, connection_pool: connection_pool)
end
end

# Raised when connection to the database could not been established because it was not
# able to connect to the host or when the authorization failed.
class DatabaseConnectionError < ConnectionNotEstablished
Expand Down

0 comments on commit 245b035

Please sign in to comment.