Skip to content

Commit

Permalink
feat: properly handles Plug.Cowboy handled throws and exits
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Aug 19, 2024
1 parent 777dc68 commit 468efaf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
62 changes: 61 additions & 1 deletion test/plug/tower_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule TowerPlugTest do
end

@tag capture_log: true
test "reports arithmetic error when a Plug.Conn IS present" do
test "reports arithmetic error during plug dispatch" do
# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/arithmetic-error"
Expand Down Expand Up @@ -42,6 +42,66 @@ defmodule TowerPlugTest do
assert Plug.Conn.request_url(plug_conn) == url
end

@tag capture_log: true
test "reports uncaught throw during plug dispatch" do
# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/uncaught-throw"

start_link_supervised!({Plug.Cowboy, plug: Tower.TestPlug, scheme: :http, port: plug_port})

{:ok, _response} = :httpc.request(url)

assert_eventually(
[
%{
id: id,
datetime: datetime,
level: :error,
kind: :throw,
reason: "something",
stacktrace: stacktrace,
plug_conn: %Plug.Conn{} = plug_conn
}
] = Tower.EphemeralReporter.events()
)

assert String.length(id) == 36
assert recent_datetime?(datetime)
assert is_list(stacktrace)
assert Plug.Conn.request_url(plug_conn) == url
end

@tag capture_log: true
test "reports abnormal exit during plug dispatch" do

Check failure on line 76 in test/plug/tower_plug_test.exs

View workflow job for this annotation

GitHub Actions / main (1.16, 26.2.5)

test reports abnormal exit during plug dispatch (TowerPlugTest)

Check failure on line 76 in test/plug/tower_plug_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

test reports abnormal exit during plug dispatch (TowerPlugTest)

Check failure on line 76 in test/plug/tower_plug_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

test reports abnormal exit during plug dispatch (TowerPlugTest)
# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/abnormal-exit"

start_link_supervised!({Plug.Cowboy, plug: Tower.TestPlug, scheme: :http, port: plug_port})

{:ok, _response} = :httpc.request(url)

assert_eventually(
[
%{
id: id,
datetime: datetime,
level: :error,
kind: :exit,
reason: :abnormal,
stacktrace: stacktrace,
plug_conn: %Plug.Conn{} = plug_conn
}
] = Tower.EphemeralReporter.events()
)

assert String.length(id) == 36
assert recent_datetime?(datetime)
assert is_list(stacktrace)
assert Plug.Conn.request_url(plug_conn) == url
end

test "reports message plug_conn manually" do
Tower.handle_message(
:info,
Expand Down
12 changes: 12 additions & 0 deletions test/support/test_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ defmodule Tower.TestPlug do
send_resp(conn, 200, "OK")
end

get "/abnormal-exit" do
exit(:abnormal)

send_resp(conn, 200, "OK")
end

get "/uncaught-throw" do
throw "something"

send_resp(conn, 200, "OK")
end

match _ do
send_resp(conn, 404, "Not Found")
end
Expand Down

0 comments on commit 468efaf

Please sign in to comment.