mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix error raised when handler doesn't exist
While working on another feature for multiple databases (auto-switching) I observed that in development the first request won't autoload the application record connection for the primary database and may not yet know about the replica connection. In my test application this caused the application to thrown an error if I tried to send the first request to the replica before the replica was connected. This wouldn't be an issue in production because the application is preloaded. In order to fix this I decided to leave the original error message and delete the new error message. I updated the original error message to include the `role` to make it a bit clearer that the connection isn't established for that particular role. The error now reads: ``` No connection pool with 'primary' found for the 'reading' role. ``` A single database application will continue uisng the original error message: ``` No connection pool with 'primary' found. ```
This commit is contained in:
parent
1fecebae31
commit
1284f826cc
4 changed files with 21 additions and 8 deletions
|
@ -1006,7 +1006,16 @@ module ActiveRecord
|
|||
# for (not necessarily the current class).
|
||||
def retrieve_connection(spec_name) #:nodoc:
|
||||
pool = retrieve_connection_pool(spec_name)
|
||||
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found." unless pool
|
||||
|
||||
unless pool
|
||||
# multiple database application
|
||||
if ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler
|
||||
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role."
|
||||
else
|
||||
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found."
|
||||
end
|
||||
end
|
||||
|
||||
pool.connection
|
||||
end
|
||||
|
||||
|
|
|
@ -158,10 +158,6 @@ 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
|
||||
|
|
|
@ -336,13 +336,13 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_calling_connected_to_on_a_non_existent_handler_raises
|
||||
error = assert_raises ArgumentError do
|
||||
error = assert_raises ActiveRecord::ConnectionNotEstablished do
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
yield
|
||||
Person.first
|
||||
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
|
||||
assert_equal "No connection pool with 'primary' found for the 'reading' role.", error.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,6 +29,14 @@ class TestUnconnectedAdapter < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_error_message_when_connection_not_established
|
||||
error = assert_raise(ActiveRecord::ConnectionNotEstablished) do
|
||||
TestRecord.find(1)
|
||||
end
|
||||
|
||||
assert_equal "No connection pool with 'primary' found.", error.message
|
||||
end
|
||||
|
||||
def test_underlying_adapter_no_longer_active
|
||||
assert_not @underlying.active?, "Removed adapter should no longer be active"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue