Skip to content

Commit

Permalink
fix: raise error on non-existant resource for api
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed May 16, 2022
1 parent ad5f1c4 commit 50f6a10
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
25 changes: 19 additions & 6 deletions lib/ash_phoenix/form/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,15 @@ defmodule AshPhoenix.Form do
"""
end

result =
case Ash.Api.resource(form.api, form.resource) do
{:ok, _} ->
:ok

{:error, error} ->
raise error
end

{original_changeset_or_query, result} =
case form.type do
:create ->
form.resource
Expand All @@ -1042,7 +1050,7 @@ defmodule AshPhoenix.Form do
changeset_opts
)
|> before_submit.()
|> form.api.create()
|> with_changeset(&form.api.create/1)

:update ->
form.original_data
Expand All @@ -1052,7 +1060,7 @@ defmodule AshPhoenix.Form do
changeset_opts
)
|> before_submit.()
|> form.api.update()
|> with_changeset(&form.api.update/1)

:destroy ->
form.original_data
Expand All @@ -1063,6 +1071,7 @@ defmodule AshPhoenix.Form do
)
|> before_submit.()
|> form.api.destroy()
|> with_changeset(&form.api.update/1)

:read ->
form.resource
Expand All @@ -1072,7 +1081,7 @@ defmodule AshPhoenix.Form do
changeset_opts
)
|> before_submit.()
|> form.api.create()
|> with_changeset(&form.api.read/1)
end

case result do
Expand All @@ -1085,7 +1094,7 @@ defmodule AshPhoenix.Form do
if opts[:raise?] do
raise Ash.Error.to_error_class(query.errors, query: query)
else
query = %{query | errors: []}
query = %{(query || original_changeset_or_query) | errors: []}

errors =
error
Expand All @@ -1107,7 +1116,7 @@ defmodule AshPhoenix.Form do
if opts[:raise?] do
raise Ash.Error.to_error_class(changeset.errors, changeset: changeset)
else
changeset = %{changeset | errors: []}
changeset = %{(changeset || original_changeset_or_query) | errors: []}

errors =
error
Expand Down Expand Up @@ -1145,6 +1154,10 @@ defmodule AshPhoenix.Form do
end
end

defp with_changeset(changeset, func) do
{changeset, func.(changeset)}
end

@doc """
Same as `submit/2`, but raises an error if the submission fails.
"""
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule AshPhoenix.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ash, ash_version("~> 1.50 and >= 1.50.8")},
{:ash, ash_version("~> 1.52.0-rc.2")},
{:phoenix, "~> 1.5.6 or ~> 1.6.0"},
{:phoenix_html, "~> 2.14 or ~> 3.0"},
{:phoenix_live_view, "~> 0.15"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"ash": {:hex, :ash, "1.51.2", "0291c99606df45ff79a8eae9bf56fe03aedbd60e2bab410979ebc6b881498001", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.9", [hex: :sourceror, repo: "hexpm", optional: false]}, {:timex, ">= 3.0.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "bc3d11c587308491a2a911bee36828da6d722c35f16e5a0078205f3fc8aace34"},
"ash": {:hex, :ash, "1.52.0-rc.2", "8c2d1a6e385821b5f8c3a4c2d2e415fa477c5e8d4fd0654c7526c07b2e948188", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: true]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.9", [hex: :sourceror, repo: "hexpm", optional: false]}, {:timex, ">= 3.0.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "5690a5da3edc8dd62d05be05446e853e329b3812e9b592013534a847646a3c80"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
Expand Down
4 changes: 2 additions & 2 deletions test/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ defmodule AshPhoenix.FormTest do
end

test "it raises an appropriate error when the incorrect api is configured" do
assert_raise RuntimeError,
~r/Resource AshPhoenix.Test.Post not found in api AshPhoenix.Test.OtherApi/,
assert_raise Ash.Error.Invalid.NoSuchResource,
~r/No such resource AshPhoenix.Test.Post/,
fn ->
Post
|> Form.for_create(:create, api: OtherApi)
Expand Down
3 changes: 0 additions & 3 deletions test/support/other_api.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
defmodule AshPhoenix.Test.OtherApi do
@moduledoc false
use Ash.Api

resources do
end
end

0 comments on commit 50f6a10

Please sign in to comment.