Skip to content

Commit 0d5b5ea

Browse files
authored
Fix case where Plug.Conn.Adapter.upgrade/3 could return any error tuple (#1340)
1 parent 575a845 commit 0d5b5ea

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎lib/plug/conn.ex‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,8 +1470,8 @@ defmodule Plug.Conn do
14701470
function; it is entirely possible (likely, even) that the server will only do the actual upgrade
14711471
later in the connection lifecycle.
14721472
1473-
If the adapter does not support the requested protocol this function will raise an
1474-
`ArgumentError`. The underlying adapter may also signal errors in the provided arguments by
1473+
If the adapter does not support the requested protocol, or if it returns any other error
1474+
for the requested upgrade, this function will raise an `ArgumentError`. The underlying adapter may also signal errors in the provided arguments by
14751475
raising; consult the corresponding adapter documentation for details.
14761476
"""
14771477
@spec upgrade_adapter(t, atom, term) :: t
@@ -1485,6 +1485,10 @@ defmodule Plug.Conn do
14851485

14861486
{:error, :not_supported} ->
14871487
raise ArgumentError, "upgrade to #{protocol} not supported by #{inspect(adapter)}"
1488+
1489+
{:error, reason} ->
1490+
raise ArgumentError,
1491+
"upgrade to #{protocol} failed in #{inspect(adapter)}: #{inspect(reason)}"
14881492
end
14891493
end
14901494

‎test/plug/conn_test.exs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,21 @@ defmodule Plug.ConnTest do
499499
)
500500
end
501501

502+
defmodule UpgradeErrorAdapter do
503+
def upgrade(_payload, _protocol, _args), do: {:error, :bad_args}
504+
end
505+
506+
test "upgrade_adapter/3 raises an error on failed upgrades" do
507+
assert_raise(
508+
ArgumentError,
509+
"upgrade to websocket failed in Plug.ConnTest.UpgradeErrorAdapter: :bad_args",
510+
fn ->
511+
%{conn(:get, "/foo") | adapter: {UpgradeErrorAdapter, nil}}
512+
|> upgrade_adapter(:websocket, opt: :bad)
513+
end
514+
)
515+
end
516+
502517
test "upgrade_adapter/3 will raise if the response is sent before upgrading" do
503518
assert_raise(Plug.Conn.AlreadySentError, fn ->
504519
conn(:get, "/foo")

0 commit comments

Comments
 (0)