Skip to content

Commit cabe311

Browse files
committed
Make DatabaseConnectionError a subclass of ConnectionNotEstablished
Before the introduction of this exception, we where raising a `ConnectionNotEstablished` and users might be rescuing that exception already. As the goal of the new exception was to be more specific, we should be subclassing `ConnectionNotEstablished` to keep the old behaviour.
1 parent 4b4895e commit cabe311

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

activerecord/lib/active_record/errors.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ class ConnectionNotEstablished < ActiveRecordError
6363
class ConnectionTimeoutError < ConnectionNotEstablished
6464
end
6565

66+
# Raised when connection to the database could not been established because it was not
67+
# able to connect to the host or when the authorization failed.
68+
class DatabaseConnectionError < ConnectionNotEstablished
69+
def initialize(message = nil)
70+
super(message || "Database connection error")
71+
end
72+
73+
class << self
74+
def hostname_error(hostname)
75+
DatabaseConnectionError.new(<<~MSG)
76+
There is an issue connecting with your hostname: #{hostname}.\n
77+
Please check your database configuration and ensure there is a valid connection to your database.
78+
MSG
79+
end
80+
81+
def username_error(username)
82+
DatabaseConnectionError.new(<<~MSG)
83+
There is an issue connecting to your database with your username/password, username: #{username}.\n
84+
Please check your database configuration to ensure the username/password are valid.
85+
MSG
86+
end
87+
end
88+
end
89+
6690
# Raised when a pool was unable to get ahold of all its connections
6791
# to perform a "group" action such as
6892
# {ActiveRecord::Base.connection_pool.disconnect!}[rdoc-ref:ConnectionAdapters::ConnectionPool#disconnect!]
@@ -238,28 +262,6 @@ def db_error(db_name)
238262
end
239263
end
240264

241-
class DatabaseConnectionError < StatementInvalid
242-
def initialize(message = nil)
243-
super(message || "Database connection error")
244-
end
245-
246-
class << self
247-
def hostname_error(hostname)
248-
DatabaseConnectionError.new(<<~MSG)
249-
There is an issue connecting with your hostname: #{hostname}.\n
250-
Please check your database configuration and ensure there is a valid connection to your database.
251-
MSG
252-
end
253-
254-
def username_error(username)
255-
DatabaseConnectionError.new(<<~MSG)
256-
There is an issue connecting to your database with your username/password, username: #{username}.\n
257-
Please check your database configuration to ensure the username/password are valid.
258-
MSG
259-
end
260-
end
261-
end
262-
263265
# Raised when creating a database if it exists.
264266
class DatabaseAlreadyExists < StatementInvalid
265267
end

0 commit comments

Comments
 (0)