Skip to content

Commit 43b6a8c

Browse files
authored
Improve error messages caused by invalid module names (#74)
Fixes #66
1 parent d69b5da commit 43b6a8c

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/phoenix/sync/predefined_shape.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,15 @@ defmodule Phoenix.Sync.PredefinedShape do
126126
end
127127

128128
defp from_queryable!(%{query: queryable} = predefined_shape) do
129-
queryable
130-
|> Electric.Client.EctoAdapter.shape_from_query!()
131-
|> from_shape_definition(predefined_shape)
129+
try do
130+
queryable
131+
|> Electric.Client.EctoAdapter.shape_from_query!()
132+
|> from_shape_definition(predefined_shape)
133+
rescue
134+
e in Protocol.UndefinedError ->
135+
raise ArgumentError,
136+
message: "Invalid query `#{inspect(queryable)}`: #{e.description}"
137+
end
132138
end
133139

134140
defp from_shape_definition(%ShapeDefinition{} = shape_definition, predefined_shape) do

test/phoenix/sync/router_test.exs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ defmodule Phoenix.Sync.RouterTest do
6363
# query version also accepts shape config
6464
sync "/query-config", Support.Todo, replica: :full
6565
sync "/query-config2", Support.Todo, replica: :full, storage: %{compaction: :disabled}
66+
67+
sync "/typo", Support.Todoo
6668
end
6769

6870
scope "/namespaced-sync", WebNamespace do
@@ -90,7 +92,7 @@ defmodule Phoenix.Sync.RouterTest do
9092
:configure_endpoint
9193
]
9294

93-
describe "Phoenix.Router - shape/2" do
95+
describe "Phoenix.Router - sync/2" do
9496
@tag table: {
9597
"todos",
9698
[
@@ -375,6 +377,15 @@ defmodule Phoenix.Sync.RouterTest do
375377

376378
assert ["0"] = Plug.Conn.get_resp_header(resp, "electric-cursor")
377379
end
380+
381+
test "raises clear error error if the schema module is not found" do
382+
assert_raise ArgumentError,
383+
~r/Invalid query `Support.Todoo`: the given module does not exist/,
384+
fn ->
385+
Phoenix.ConnTest.build_conn()
386+
|> Phoenix.ConnTest.get("/sync/typo", %{offset: "-1"})
387+
end
388+
end
378389
end
379390

380391
describe "Plug.Router - shape/2" do

test/support/endpoint.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ defmodule Phoenix.Sync.LiveViewTest.Endpoint do
3939
Exception.message(conn.reason)
4040
end
4141
end
42+
43+
defmodule Phoenix.ErrorView do
44+
def render("500.html", %{reason: exception}) do
45+
Exception.message(exception)
46+
end
47+
end

0 commit comments

Comments
 (0)