Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: properly handles Plug.Cowboy handled throws and exits #41

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,11 +5,23 @@
plug(:dispatch)

get "/arithmetic-error" do
1 / 0

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the result of evaluating operator '/'/2 is ignored (suppress the warning by assigning the expression to the _ variable)

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the result of evaluating operator '/'/2 is ignored (suppress the warning by assigning the expression to the _ variable)

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the result of evaluating operator '/'/2 is ignored (suppress the warning by assigning the expression to the _ variable)

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the result of evaluating operator '/'/2 is ignored (suppress the warning by assigning the expression to the _ variable)

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the result of evaluating operator '/'/2 is ignored (suppress the warning by assigning the expression to the _ variable)

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 8 in test/support/test_plug.ex

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the result of evaluating operator '/'/2 is ignored (suppress the warning by assigning the expression to the _ variable)

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
Loading