Skip to content

Commit 7d8bb3a

Browse files
committed
Print loaded params for unexpected route failure
1 parent bb87ba2 commit 7d8bb3a

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ end)
106106
TestServer.plug(fn conn ->
107107
{:ok, body, _conn} = Plug.Conn.read_body(conn, [])
108108

109-
Map.put(conn, :request_body, body)
109+
%{conn | body_params: Jason.decode!(body)}
110110
end)
111111

112112
TestServer.plug(MyPlug)

lib/test_server.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ defmodule TestServer do
186186

187187
message =
188188
case function_accepts_instance_arg do
189-
true -> "Multiple #{inspect(Instance)}'s running, please pass instance to `#{inspect(m)}.#{f}/#{a}`."
190-
false -> "Multiple #{inspect(Instance)}'s running."
189+
true ->
190+
"Multiple #{inspect(Instance)}'s running, please pass instance to `#{inspect(m)}.#{f}/#{a}`."
191+
192+
false ->
193+
"Multiple #{inspect(Instance)}'s running."
191194
end
192195

193196
raise """

lib/test_server/instance.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ defmodule TestServer.Instance do
3333
end
3434

3535
@spec dispatch(pid(), Plug.Conn.t()) ::
36-
{:ok, Plug.Conn.t()} | {:error, :not_found} | {:error, {term(), list()}}
36+
{:ok, Plug.Conn.t()}
37+
| {:error, {:not_found, Plug.Conn.t()}}
38+
| {:error, {term(), list()}}
3739
def dispatch(instance, conn) do
3840
GenServer.call(instance, {:dispatch, conn})
3941
end
@@ -51,8 +53,6 @@ defmodule TestServer.Instance do
5153
end
5254

5355
@spec format_routes([map()]) :: binary()
54-
def format_routes([]), do: "None"
55-
5656
def format_routes(routes) do
5757
routes
5858
|> Enum.with_index()
@@ -229,7 +229,7 @@ defmodule TestServer.Instance do
229229
|> Enum.find_index(&matches?(&1, conn))
230230
|> case do
231231
nil ->
232-
{{:error, :not_found}, state}
232+
{{:error, {:not_found, conn}}, state}
233233

234234
index ->
235235
%{to: plug, stacktrace: stacktrace} = Enum.at(state.routes, index)

lib/test_server/plug_cowboy.ex

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,41 @@ defmodule TestServer.Plug.Cowboy do
112112
{:ok, conn} ->
113113
conn
114114

115-
{:error, :not_found} ->
116-
resp_error(
117-
conn,
118-
instance,
119-
{RuntimeError.exception("""
120-
Unexpected #{conn.method} request received at #{conn.request_path}.
115+
{:error, {:not_found, conn}} ->
116+
message =
117+
"Unexpected #{conn.method} request received at #{conn.request_path}"
118+
|> append_params(conn)
119+
|> format_active_routes(Instance.active_routes(instance), instance)
121120

122-
Active routes for request:
123-
124-
#{Instance.format_routes(Instance.active_routes(instance))}
125-
"""), []}
126-
)
121+
resp_error(conn, instance, {RuntimeError.exception(message), []})
127122

128123
{:error, {error, stacktrace}} ->
129124
resp_error(conn, instance, {error, stacktrace})
130125
end
131126
end
132127

128+
defp append_params(message, conn) do
129+
conn
130+
|> Map.take([:query_params, :body_params])
131+
|> Enum.filter(fn
132+
{_key, %Conn.Unfetched{}} -> false
133+
{_key, empty} when empty == %{} -> false
134+
{_key, params} when is_map(params) -> true
135+
end)
136+
|> case do
137+
[] -> message <> "."
138+
params -> message <> " with params:\n\n#{inspect(Map.new(params), pretty: true)}"
139+
end
140+
end
141+
142+
defp format_active_routes(message, [], instance),
143+
do: message <> "\n\nNo active routes for #{inspect(Instance)} #{inspect(instance)}"
144+
145+
defp format_active_routes(message, active_routes, instance) do
146+
message <>
147+
"\n\nActive routes for #{inspect(Instance)} #{inspect(instance)}:\n\n#{Instance.format_routes(active_routes)}"
148+
end
149+
133150
defp resp_error(conn, instance, {exception, stacktrace}) do
134151
Instance.report_error(instance, {exception, stacktrace})
135152

test/test_server_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ defmodule TestServerTest do
242242
assert :ok = TestServer.add("/")
243243

244244
assert {:ok, _} = unquote(__MODULE__).request(TestServer.url("/"))
245-
assert {:error, _} = unquote(__MODULE__).request(TestServer.url("/"))
245+
assert {:error, _} = unquote(__MODULE__).request(TestServer.url("/?a=1"))
246246
end
247247
end
248248

249-
assert capture_io(fn -> ExUnit.run() end) =~ "Unexpected GET request received at /."
249+
assert captured_io = capture_io(fn -> ExUnit.run() end)
250+
assert captured_io =~ "Unexpected GET request received at / with params:"
251+
assert captured_io =~ "query_params: %{\"a\" => \"1\"}"
250252
end
251253

252254
test "with no requests" do

0 commit comments

Comments
 (0)