Skip to content

Commit 6305bd7

Browse files
authored
Merge pull request rails#50104 from joshuay03/improve-adapter-not-found-error-message
Improve `AdapterNotFound` error message
2 parents 438cad4 + 8174f94 commit 6305bd7

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

activerecord/lib/active_record/connection_adapters.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,32 @@ def register(name, class_name, path = class_name.underscore)
2323

2424
def resolve(adapter_name) # :nodoc:
2525
# Require the adapter itself and give useful feedback about
26-
# 1. Missing adapter gems and
27-
# 2. Adapter gems' missing dependencies.
26+
# 1. Missing adapter gems.
27+
# 2. Incorrectly registered adapters.
28+
# 3. Adapter gems' missing dependencies.
2829
class_name, path_to_adapter = @adapters[adapter_name]
2930

3031
unless class_name
31-
raise AdapterNotFound, "database configuration specifies nonexistent '#{adapter_name}' adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile."
32+
raise AdapterNotFound, <<~MSG.squish
33+
Database configuration specifies nonexistent '#{adapter_name}' adapter.
34+
Available adapters are: #{@adapters.keys.sort.join(", ")}.
35+
Ensure that the adapter is spelled correctly in config/database.yml or that you've added the necessary
36+
adapter gem to your Gemfile if it's not in the list of available adapters.
37+
MSG
3238
end
3339

3440
unless Object.const_defined?(class_name)
3541
begin
3642
require path_to_adapter
3743
rescue LoadError => error
38-
# We couldn't require the adapter itself. Raise an exception that
39-
# points out config typos and missing gems.
44+
# We couldn't require the adapter itself.
4045
if error.path == path_to_adapter
41-
# We can assume that a non-builtin adapter was specified, so it's
42-
# either misspelled or missing from Gemfile.
43-
raise LoadError, "Error loading the '#{adapter_name}' Active Record adapter. Ensure that the necessary adapter gem is in the Gemfile. #{error.message}", error.backtrace
44-
46+
# We can assume here that a non-builtin adapter was specified and the path
47+
# registered by the adapter gem is incorrect.
48+
raise LoadError, "Error loading the '#{adapter_name}' Active Record adapter. Ensure that the path registered by the adapter gem is correct. #{error.message}", error.backtrace
49+
else
4550
# Bubbled up from the adapter require. Prefix the exception message
4651
# with some guidance about how to address it and reraise.
47-
else
4852
raise LoadError, "Error loading the '#{adapter_name}' Active Record adapter. Missing a gem it depends on? #{error.message}", error.backtrace
4953
end
5054
end

activerecord/test/cases/database_configurations/resolver_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_url_invalid_adapter
1616
Base.connection_handler.establish_connection "ridiculous://foo?encoding=utf8"
1717
end
1818

19-
assert_match "database configuration specifies nonexistent 'ridiculous' adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.", error.message
19+
assert_match "Database configuration specifies nonexistent 'ridiculous' adapter. Available adapters are: abstract, fake, mysql2, postgresql, sqlite3, trilogy. Ensure that the adapter is spelled correctly in config/database.yml or that you've added the necessary adapter gem to your Gemfile if it's not in the list of available adapters.", error.message
2020
end
2121

2222
# The abstract adapter is used simply to bypass the bit of code that

railties/test/commands/dbconsole_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_start
120120
def test_unknown_command_line_client
121121
start(adapter: "unknown", database: "db")
122122
assert aborted
123-
assert_match(/database configuration specifies nonexistent 'unknown' adapter/, output)
123+
assert_match(/Database configuration specifies nonexistent 'unknown' adapter/, output)
124124
end
125125

126126
def test_primary_is_automatically_picked_with_3_level_configuration

0 commit comments

Comments
 (0)