Skip to content

Commit

Permalink
WIP Phoenix.ActionClauseError
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Oct 31, 2024
1 parent c0c8134 commit 30de7fb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion test/support/phoenix_app/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Tower.PhoenixApp.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_app

plug(Tower.TestPlug)
plug Plug.Parsers, parsers: [] # So that it fetches query params
plug(Tower.PhoenixApp.Router)
end
4 changes: 2 additions & 2 deletions test/support/phoenix_app/error_html.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Tower.PhoenixApp.ErrorHTML do
def render("500.html", _assigns) do
"Internal Server Error"
def render(template, _assigns) do
Phoenix.Controller.status_message_from_template(template)
end
end
25 changes: 25 additions & 0 deletions test/support/phoenix_app/home_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Tower.PhoenixApp.HomeController do
use Phoenix.Controller, formats: [:html], put_default_views: false

def show(conn, %{"param" => "valid"}) do
Plug.Conn.send_resp(conn, 200, "Hey!")
end

def runtime_error(conn, _params) do
raise "an error"

Plug.Conn.send_resp(conn, 200, "OK")
end

def abnormal_exit(conn, _params) do
exit(:abnormal)

Plug.Conn.send_resp(conn, 200, "OK")
end

def uncaught_throw(conn, _params) do
throw("something")

Plug.Conn.send_resp(conn, 200, "OK")
end
end
8 changes: 8 additions & 0 deletions test/support/phoenix_app/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Tower.PhoenixApp.Router do
use Phoenix.Router, helpers: false

get("/runtime-error", Tower.PhoenixApp.HomeController, :runtime_error)
get("/abnormal-exit", Tower.PhoenixApp.HomeController, :abnormal_exit)
get("/uncaught-throw", Tower.PhoenixApp.HomeController, :uncaught_throw)
get("/show", Tower.PhoenixApp.HomeController, :show)
end
35 changes: 33 additions & 2 deletions test/tower/phoenix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TowerPhoenixTest do

import ExUnit.CaptureLog, only: [capture_log: 1]

setup do
setup context do
on_exit(fn ->
Tower.EphemeralReporter.reset()
end)
Expand All @@ -17,7 +17,11 @@ defmodule TowerPhoenixTest do
Application.put_env(
:phoenix_app,
Tower.PhoenixApp.Endpoint,
adapter: Bandit.PhoenixAdapter,
adapter:
case context[:adapter] do
:bandit -> Bandit.PhoenixAdapter
:cowboy -> Phoenix.Endpoint.Cowboy2Adapter
end,
server: true,
http: [port: port],
url: [scheme: "http", port: port, host: host],
Expand All @@ -31,6 +35,7 @@ defmodule TowerPhoenixTest do
%{base_url: "http://#{host}:#{port}"}
end

@tag adapter: :bandit
test "reports runtime error during Phoenix.Endpoint dispatch with Bandit", %{base_url: base_url} do
url = base_url <> "/runtime-error"

Expand Down Expand Up @@ -58,6 +63,7 @@ defmodule TowerPhoenixTest do
assert Plug.Conn.request_url(plug_conn) == url
end

@tag adapter: :bandit
test "reports uncaught throw during Phoenix.Endpoint dispatch with Bandit", %{
base_url: base_url
} do
Expand Down Expand Up @@ -89,6 +95,7 @@ defmodule TowerPhoenixTest do
assert [] = stacktrace
end

@tag adapter: :bandit
test "reports abnormal exit during Phoenix.Endpoint dispatch with Bandit", %{base_url: base_url} do
capture_log(fn ->
{:ok, {{_, 500, _}, _, _}} = :httpc.request(base_url <> "/abnormal-exit")
Expand All @@ -114,6 +121,30 @@ defmodule TowerPhoenixTest do
assert [_ | _] = stacktrace
end

@tag adapter: :cowboy
test "doesn't report exceptions that return 4xx status codes with Cowboy", %{base_url: base_url} do
# Forcing Phoenix.ActionClauseError
url = base_url <> "/show?param=invalid"

capture_log(fn ->
{:ok, {{_, 400, _}, _, _}} = :httpc.request(url)
end)

assert [] = Tower.EphemeralReporter.events()
end

@tag adapter: :bandit
test "doesn't report exceptions that return 4xx status codes with Bandit", %{base_url: base_url} do

Check failure on line 137 in test/tower/phoenix_test.exs

View workflow job for this annotation

GitHub Actions / main (1.16, 26.2.5.4)

test doesn't report exceptions that return 4xx status codes with Bandit (TowerPhoenixTest)

Check failure on line 137 in test/tower/phoenix_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.15)

test doesn't report exceptions that return 4xx status codes with Bandit (TowerPhoenixTest)

Check failure on line 137 in test/tower/phoenix_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

test doesn't report exceptions that return 4xx status codes with Bandit (TowerPhoenixTest)

Check failure on line 137 in test/tower/phoenix_test.exs

View workflow job for this annotation

GitHub Actions / main (1.16, 26.2.5.4)

test doesn't report exceptions that return 4xx status codes with Bandit (TowerPhoenixTest)

Check failure on line 137 in test/tower/phoenix_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.15)

test doesn't report exceptions that return 4xx status codes with Bandit (TowerPhoenixTest)

Check failure on line 137 in test/tower/phoenix_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

test doesn't report exceptions that return 4xx status codes with Bandit (TowerPhoenixTest)
# Forcing Phoenix.ActionClauseError
url = base_url <> "/show?param=invalid"

capture_log(fn ->
{:ok, {{_, 400, _}, _, _}} = :httpc.request(url)
end)

assert [] = Tower.EphemeralReporter.events()
end

defp recent_datetime?(datetime) do
diff =
:logger.timestamp()
Expand Down

0 comments on commit 30de7fb

Please sign in to comment.