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

Merge pull request #34753 from eileencodes/raise-less-confusing-error-if-handler-doesnt-exist

Raise helpful error when role doesn't exist
This commit is contained in:
Eileen M. Uchitelle 2018-12-21 11:43:35 -05:00 committed by GitHub
commit b00e46bd6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View file

@ -158,6 +158,10 @@ module ActiveRecord
end
def with_handler(handler_key, &blk) # :nodoc:
unless ActiveRecord::Base.connection_handlers.keys.include?(handler_key)
raise ArgumentError, "The #{handler_key} role does not exist. Add it by establishing a connection with `connects_to` or use an existing role (#{ActiveRecord::Base.connection_handlers.keys.join(", ")})."
end
handler = lookup_connection_handler(handler_key)
swap_connection_handler(handler, &blk)
end

View file

@ -328,6 +328,16 @@ module ActiveRecord
ensure
ActiveRecord::Base.connection_handlers = original_handlers
end
def test_calling_connected_to_on_a_non_existent_handler_raises
error = assert_raises ArgumentError do
ActiveRecord::Base.connected_to(role: :reading) do
yield
end
end
assert_equal "The reading role does not exist. Add it by establishing a connection with `connects_to` or use an existing role (writing).", error.message
end
end
end
end

View file

@ -56,6 +56,11 @@ class QueryCacheTest < ActiveRecord::TestCase
end
def test_query_cache_is_applied_to_connections_in_all_handlers
ActiveRecord::Base.connection_handlers = {
writing: ActiveRecord::Base.default_connection_handler,
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
}
ActiveRecord::Base.connected_to(role: :reading) do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["arunit"])
end