1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Distinguish missing adapter gems from load errors within the adapter

* When the adapter is missing, raise an exception that points out config
  typos and missing Gemfile entries. (We can assume that a non-builtin
  adapter was used since these are always available.)
* When loading an adapter raises a LoadError, prefix its error message
  to indicate that the adapter is likely missing an optional dependency.
This commit is contained in:
Jeremy Daer 2017-09-29 18:24:24 -07:00
parent 7c564d8740
commit 55a2c101b2
No known key found for this signature in database
GPG key ID: AB8F6399D5C60664
3 changed files with 32 additions and 7 deletions

View file

@ -25,13 +25,26 @@ module ActionCable
# Also makes sure proper dependencies are required. # Also makes sure proper dependencies are required.
def pubsub_adapter def pubsub_adapter
adapter = (cable.fetch("adapter") { "redis" }) adapter = (cable.fetch("adapter") { "redis" })
# Require the adapter itself and give useful feedback about
# 1. Missing adapter gems and
# 2. Adapter gems' missing dependencies.
path_to_adapter = "action_cable/subscription_adapter/#{adapter}" path_to_adapter = "action_cable/subscription_adapter/#{adapter}"
begin begin
require path_to_adapter require path_to_adapter
rescue Gem::LoadError => e
raise Gem::LoadError, "Specified '#{adapter}' for Action Cable pubsub adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by Action Cable)."
rescue LoadError => e rescue LoadError => e
raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/cable.yml is valid. If you use an adapter other than 'postgresql' or 'redis' add the necessary adapter gem to the Gemfile.", e.backtrace # We couldn't require the adapter itself. Raise an exception that
# points out config typos and missing gems.
if e.path == path_to_adapter
# We can assume that a non-builtin adapter was specified, so it's
# either misspelled or missing from Gemfile.
raise e.class, "Could not load the '#{adapter}' Action Cable pubsub adapter. Ensure that the adapter is spelled correctly in config/cable.yml and that you've added the necessary adapter gem to your Gemfile.", e.backtrace
# Bubbled up from the adapter require. Prefix the exception message
# with some guidance about how to address it and reraise.
else
raise e.class, "Error loading the '#{adapter}' Action Cable pubsub adapter. Missing a gem it depends on? #{e.message}", e.backtrace
end
end end
adapter = adapter.camelize adapter = adapter.camelize

View file

@ -183,13 +183,25 @@ module ActiveRecord
raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter) raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
# Require the adapter itself and give useful feedback about
# 1. Missing adapter gems and
# 2. Adapter gems' missing dependencies.
path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter" path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
begin begin
require path_to_adapter require path_to_adapter
rescue Gem::LoadError => e
raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
rescue LoadError => e rescue LoadError => e
raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace # We couldn't require the adapter itself. Raise an exception that
# points out config typos and missing gems.
if e.path == path_to_adapter
# We can assume that a non-builtin adapter was specified, so it's
# either misspelled or missing from Gemfile.
raise e.class, "Could not load the '#{spec[:adapter]}' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.", e.backtrace
# Bubbled up from the adapter require. Prefix the exception message
# with some guidance about how to address it and reraise.
else
raise e.class, "Error loading the '#{spec[:adapter]}' Action Record adapter. Missing a gem it depends on? #{e.message}", e.backtrace
end
end end
adapter_method = "#{spec[:adapter]}_connection" adapter_method = "#{spec[:adapter]}_connection"

View file

@ -19,7 +19,7 @@ module ActiveRecord
spec "ridiculous://foo?encoding=utf8" spec "ridiculous://foo?encoding=utf8"
end end
assert_match "Could not load 'active_record/connection_adapters/ridiculous_adapter'", error.message assert_match "Could not load the 'ridiculous' Active Record 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
end end
# The abstract adapter is used simply to bypass the bit of code that # The abstract adapter is used simply to bypass the bit of code that