Skip to content

Commit cebf20c

Browse files
authored
Translate errors for Cowboy 2.13.0
* Add a clause for the Cowboy 2.13.0 error format The error format between Cowboy 2.12 and below, and Cowboy 1.13 was changed in this commit. Since we support both versions of Cowboy for now, an extra clause has been added to handle this format. ninenines/cowboy@fbd680f * Run CI against latest cowboy version in a separate stage * fix child spec tests for newer ranch versions
1 parent 79b7bf8 commit cebf20c

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,23 @@ jobs:
4444
- run: mix compile --warnings-as-errors
4545
if: ${{ matrix.lint }}
4646

47+
- run: mix test
48+
test_cowboy_latest:
49+
runs-on: ubuntu-20.04
50+
env:
51+
MIX_ENV: test
52+
strategy:
53+
fail-fast: false
54+
steps:
55+
- uses: actions/checkout@v2
56+
57+
- uses: erlef/setup-beam@v1
58+
with:
59+
otp-version: 24.2.2
60+
elixir-version: 1.13.3
61+
62+
- run: mix deps.unlock cowboy cowlib ranch && mix deps.get --only test
63+
64+
- run: mix deps.compile
65+
4766
- run: mix test

lib/plug/cowboy/translator.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Plug.Cowboy.Translator do
22
@moduledoc false
33

4+
# Cowboy 2.12.0 and below error format
45
@doc """
56
The `translate/4` function expected by custom Logger translators.
67
"""
@@ -14,6 +15,17 @@ defmodule Plug.Cowboy.Translator do
1415
translate_ranch(min_level, ref, extra, stream_pid, reason, stack)
1516
end
1617

18+
# Cowboy 2.13.0 error format
19+
def translate(
20+
min_level,
21+
:error,
22+
:format,
23+
{~c"Ranch listener" ++ _, [ref, conn_pid, stream_id, stream_pid, {reason, stack}]}
24+
) do
25+
extra = [" (connection ", inspect(conn_pid), ", stream id ", inspect(stream_id), ?)]
26+
translate_ranch(min_level, ref, extra, stream_pid, reason, stack)
27+
end
28+
1729
def translate(_min_level, _level, _kind, _data) do
1830
:none
1931
end

test/plug/cowboy_test.exs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,20 @@ defmodule Plug.CowboyTest do
1313
test "supports Elixir child specs" do
1414
spec = {Plug.Cowboy, [scheme: :http, plug: __MODULE__, port: 4040]}
1515

16+
ranch_listener_mod = ranch_listener_for_version()
17+
1618
assert %{
17-
id: {:ranch_listener_sup, Plug.CowboyTest.HTTP},
18-
modules: [:ranch_listener_sup],
19-
restart: :permanent,
20-
shutdown: :infinity,
21-
start: {:ranch_listener_sup, :start_link, _},
19+
id: {^ranch_listener_mod, Plug.CowboyTest.HTTP},
20+
start: {^ranch_listener_mod, :start_link, _},
2221
type: :supervisor
2322
} = Supervisor.child_spec(spec, [])
2423

2524
# For backwards compatibility:
2625
spec = {Plug.Cowboy, [scheme: :http, plug: __MODULE__, options: [port: 4040]]}
2726

2827
assert %{
29-
id: {:ranch_listener_sup, Plug.CowboyTest.HTTP},
30-
modules: [:ranch_listener_sup],
31-
restart: :permanent,
32-
shutdown: :infinity,
33-
start: {:ranch_listener_sup, :start_link, _},
28+
id: {^ranch_listener_mod, Plug.CowboyTest.HTTP},
29+
start: {^ranch_listener_mod, :start_link, _},
3430
type: :supervisor
3531
} = Supervisor.child_spec(spec, [])
3632

@@ -39,11 +35,8 @@ defmodule Plug.CowboyTest do
3935
[scheme: :http, plug: __MODULE__, parent: :key, options: [:inet6, port: 4040]]}
4036

4137
assert %{
42-
id: {:ranch_listener_sup, Plug.CowboyTest.HTTP},
43-
modules: [:ranch_listener_sup],
44-
restart: :permanent,
45-
shutdown: :infinity,
46-
start: {:ranch_listener_sup, :start_link, _},
38+
id: {^ranch_listener_mod, Plug.CowboyTest.HTTP},
39+
start: {^ranch_listener_mod, :start_link, _},
4740
type: :supervisor
4841
} = Supervisor.child_spec(spec, [])
4942
end
@@ -58,7 +51,8 @@ defmodule Plug.CowboyTest do
5851

5952
spec = {Plug.Cowboy, [scheme: :https, plug: __MODULE__] ++ options}
6053

61-
%{start: {:ranch_listener_sup, :start_link, opts}} = Supervisor.child_spec(spec, [])
54+
ranch_listener_mod = ranch_listener_for_version()
55+
%{start: {^ranch_listener_mod, :start_link, opts}} = Supervisor.child_spec(spec, [])
6256

6357
assert [
6458
Plug.CowboyTest.HTTPS,
@@ -178,13 +172,19 @@ defmodule Plug.CowboyTest do
178172
end
179173

180174
test "builds child specs" do
175+
ranch_listener_mod = ranch_listener_for_version()
176+
181177
assert %{
182-
id: {:ranch_listener_sup, Plug.CowboyTest.HTTP},
183-
modules: [:ranch_listener_sup],
184-
start: {:ranch_listener_sup, :start_link, _},
185-
restart: :permanent,
186-
shutdown: :infinity,
178+
id: {^ranch_listener_mod, Plug.CowboyTest.HTTP},
179+
start: {^ranch_listener_mod, :start_link, _},
187180
type: :supervisor
188181
} = child_spec(scheme: :http, plug: __MODULE__, options: [])
189182
end
183+
184+
defp ranch_listener_for_version() do
185+
case Version.parse!("#{Application.spec(:ranch, :vsn)}") |> Version.compare("2.2.0") do
186+
:lt -> :ranch_listener_sup
187+
_ -> :ranch_embedded_sup
188+
end
189+
end
190190
end

0 commit comments

Comments
 (0)