Skip to content

Commit ca36795

Browse files
authored
Wrap errors with Plug.Conn.Wrapper error to support live reload (#190)
1 parent 7329d30 commit ca36795

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

lib/phoenix_ecto/check_repo_status.ex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ defmodule Phoenix.Ecto.CheckRepoStatus do
2727
end
2828

2929
def call(%Conn{} = conn, opts) do
30-
repos = Application.get_env(opts[:otp_app], :ecto_repos, [])
30+
try do
31+
repos = Application.get_env(opts[:otp_app], :ecto_repos, [])
3132

32-
for repo <- repos, Process.whereis(repo) do
33-
check_pending_migrations!(repo, opts) || check_storage_up!(repo)
34-
end
33+
for repo <- repos, Process.whereis(repo) do
34+
check_pending_migrations!(repo, opts) || check_storage_up!(repo)
35+
end
3536

36-
conn
37+
conn
38+
rescue
39+
err in [Phoenix.Ecto.StorageNotCreatedError, Phoenix.Ecto.PendingMigrationError] ->
40+
Plug.Conn.WrapperError.reraise(conn, :error, err, __STACKTRACE__)
41+
end
3742
end
3843

3944
defp check_storage_up!(repo) do

test/phoenix_ecto/check_repo_status_test.exs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
44

55
alias Phoenix.Ecto.CheckRepoStatus
66

7+
defmacro assert_wrapped(kind, func) do
8+
quote do
9+
wrapper_error = assert_raise(Plug.Conn.WrapperError, unquote(func))
10+
assert %unquote(kind){} = wrapper_error.reason
11+
wrapper_error.reason
12+
end
13+
end
14+
715
defmodule LongLivedProcess do
816
def run do
917
Process.sleep(1_000)
@@ -62,7 +70,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
6270

6371
conn = conn(:get, "/")
6472

65-
assert_raise(Phoenix.Ecto.StorageNotCreatedError, fn ->
73+
assert_wrapped(Phoenix.Ecto.StorageNotCreatedError, fn ->
6674
CheckRepoStatus.call(
6775
conn,
6876
otp_app: :check_repo_ready,
@@ -82,7 +90,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
8290
conn = conn(:get, "/")
8391

8492
exception =
85-
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
93+
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
8694
CheckRepoStatus.call(
8795
conn,
8896
otp_app: :check_repo_ready,
@@ -104,7 +112,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
104112
conn = conn(:get, "/")
105113

106114
exception =
107-
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
115+
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
108116
CheckRepoStatus.call(
109117
conn,
110118
otp_app: :check_repo_ready,
@@ -134,7 +142,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
134142
conn = conn(:get, "/")
135143

136144
exception =
137-
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
145+
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
138146
CheckRepoStatus.call(
139147
conn,
140148
otp_app: :check_repo_ready,
@@ -159,7 +167,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
159167
mock_migrations_fn = fn _repo, ["foo"], _opts -> [{:down, 1, "migration"}] end
160168

161169
exception =
162-
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
170+
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
163171
CheckRepoStatus.call(
164172
conn,
165173
otp_app: :check_repo_ready,
@@ -174,7 +182,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
174182
mock_migrations_fn = fn _repo, ["foo", "bar"], _opts -> [{:down, 1, "migration"}] end
175183

176184
exception =
177-
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
185+
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
178186
CheckRepoStatus.call(
179187
conn,
180188
otp_app: :check_repo_ready,
@@ -261,7 +269,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
261269

262270
conn = conn(:get, "/")
263271

264-
assert_raise(Phoenix.Ecto.StorageNotCreatedError, fn ->
272+
assert_wrapped(Phoenix.Ecto.StorageNotCreatedError, fn ->
265273
CheckRepoStatus.call(
266274
conn,
267275
otp_app: :check_repo_ready,
@@ -287,7 +295,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
287295
conn = conn(:get, "/")
288296

289297
exception =
290-
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
298+
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
291299
CheckRepoStatus.call(
292300
conn,
293301
otp_app: :check_repo_ready,

0 commit comments

Comments
 (0)