Skip to content

Commit 946a01d

Browse files
committed
More explicit error handling
1 parent 7a6b961 commit 946a01d

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.1.2 (TBA)
2+
3+
- Better formatting of errors
4+
15
## v0.1.1 (2022-09-11)
26

37
- `TestServer.url/2` no longer autostarts the test server

lib/test_server.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ defmodule TestServer do
8787

8888
routes ->
8989
raise """
90-
The test ended before the following #{inspect(__MODULE__)} route(s) received a request:
90+
The test ended before the following #{inspect(Instance)} route(s) received a request:
9191
92-
#{Instance.format_routes(routes)}
92+
#{Instance.format_routes(routes)}
9393
"""
9494
end
9595
end

lib/test_server/instance.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule TestServer.Instance do
4444

4545
@spec format_routes([map()]) :: binary()
4646
def format_routes([]), do: "None"
47+
4748
def format_routes(routes) do
4849
routes
4950
|> Enum.with_index()
@@ -55,15 +56,16 @@ defmodule TestServer.Instance do
5556
end)
5657
end
5758

58-
@spec report_error(pid(), binary()) :: :ok
59-
def report_error(instance, message) do
59+
@spec report_error(pid(), {struct(), list()}) :: :ok
60+
def report_error(instance, {exception, stacktrace}) do
6061
options = get_options(instance)
6162
caller = Keyword.fetch!(options, :caller)
6263

63-
unless Keyword.get(options, :suppress_warning, false), do: IO.warn(message)
64+
unless Keyword.get(options, :suppress_warning, false),
65+
do: IO.warn(Exception.format(:error, exception, stacktrace))
6466

6567
ExUnit.OnExitHandler.add(caller, make_ref(), fn ->
66-
raise message
68+
reraise exception, stacktrace
6769
end)
6870

6971
:ok

lib/test_server/plug_cowboy.ex

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,24 @@ defmodule TestServer.Plug.Cowboy do
116116
resp_error(
117117
conn,
118118
instance,
119-
"""
120-
Unexpected #{conn.method} request received at #{conn.request_path}.
119+
{RuntimeError.exception("""
120+
Unexpected #{conn.method} request received at #{conn.request_path}.
121121
122-
Active routes for request:
122+
Active routes for request:
123123
124-
#{Instance.format_routes(Instance.active_routes(instance))}
125-
"""
124+
#{Instance.format_routes(Instance.active_routes(instance))}
125+
"""), []}
126126
)
127127

128128
{:error, {error, stacktrace}} ->
129129
resp_error(conn, instance, {error, stacktrace})
130130
end
131131
end
132132

133-
defp resp_error(conn, instance, {error, stacktrace}) do
134-
resp_error(
135-
conn,
136-
instance,
137-
"""
138-
#{inspect error}
133+
defp resp_error(conn, instance, {exception, stacktrace}) do
134+
Instance.report_error(instance, {exception, stacktrace})
139135

140-
#{Exception.format_stacktrace(stacktrace)}
141-
"""
142-
)
143-
end
144-
145-
defp resp_error(conn, instance, message) do
146-
Instance.report_error(instance, message)
147-
148-
Conn.send_resp(conn, 500, to_string(message))
136+
Conn.send_resp(conn, 500, Exception.format(:error, exception, stacktrace))
149137
end
150138
end
151139
end

test/test_server_test.exs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ defmodule TestServerTest do
163163
end
164164

165165
assert capture_io(fn -> ExUnit.run() end) =~
166-
"The test ended before the following TestServer route(s) received a request"
166+
"The test ended before the following TestServer.Instance route(s) received a request"
167167
end
168168

169169
test "with callback plug" do
@@ -177,6 +177,23 @@ defmodule TestServerTest do
177177
assert request(TestServer.url("/")) == {:ok, to_string(MyPlug)}
178178
end
179179

180+
test "with callback function raising exception" do
181+
defmodule ToFunctionRaiseTest do
182+
use ExUnit.Case
183+
184+
test "fails" do
185+
{:ok, _instance} = TestServer.start(suppress_warning: true)
186+
187+
assert :ok = TestServer.add("/", to: fn _conn -> raise "boom" end)
188+
assert {:ok, _} = unquote(__MODULE__).request(TestServer.url("/"))
189+
end
190+
end
191+
192+
assert io = capture_io(fn -> ExUnit.run() end)
193+
assert io =~ "(RuntimeError) boom"
194+
assert io =~ "anonymous fn/1 in TestServerTest.ToFunctionRaiseTest"
195+
end
196+
180197
test "with callback function" do
181198
assert :ok =
182199
TestServer.add("/",

0 commit comments

Comments
 (0)