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 0a2deb9 commit 350d36a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/tower/logger_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ defmodule Tower.LoggerHandler do
Tower.handle_exit(exit_reason, stacktrace, log_event: log_event)
end

def log(%{level: :error, meta: %{crash_reason: exit_reason}} = log_event, _config) do
Tower.handle_exit(exit_reason, [], log_event: log_event)
end

def log(%{level: level, msg: {:string, reason_chardata}} = log_event, _config) do
if should_handle?(level) do
Tower.handle_message(level, IO.chardata_to_string(reason_chardata), log_event: log_event)
Expand Down
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
# 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 350d36a

Please sign in to comment.