fix: raise error on non-existant resource for api

This commit is contained in:
Zach Daniel 2022-05-16 16:09:38 -04:00
parent ad5f1c4b5e
commit 50f6a108b4
5 changed files with 23 additions and 13 deletions

View file

@ -1032,7 +1032,15 @@ defmodule AshPhoenix.Form do
""" """
end 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 case form.type do
:create -> :create ->
form.resource form.resource
@ -1042,7 +1050,7 @@ defmodule AshPhoenix.Form do
changeset_opts changeset_opts
) )
|> before_submit.() |> before_submit.()
|> form.api.create() |> with_changeset(&form.api.create/1)
:update -> :update ->
form.original_data form.original_data
@ -1052,7 +1060,7 @@ defmodule AshPhoenix.Form do
changeset_opts changeset_opts
) )
|> before_submit.() |> before_submit.()
|> form.api.update() |> with_changeset(&form.api.update/1)
:destroy -> :destroy ->
form.original_data form.original_data
@ -1063,6 +1071,7 @@ defmodule AshPhoenix.Form do
) )
|> before_submit.() |> before_submit.()
|> form.api.destroy() |> form.api.destroy()
|> with_changeset(&form.api.update/1)
:read -> :read ->
form.resource form.resource
@ -1072,7 +1081,7 @@ defmodule AshPhoenix.Form do
changeset_opts changeset_opts
) )
|> before_submit.() |> before_submit.()
|> form.api.create() |> with_changeset(&form.api.read/1)
end end
case result do case result do
@ -1085,7 +1094,7 @@ defmodule AshPhoenix.Form do
if opts[:raise?] do if opts[:raise?] do
raise Ash.Error.to_error_class(query.errors, query: query) raise Ash.Error.to_error_class(query.errors, query: query)
else else
query = %{query | errors: []} query = %{(query || original_changeset_or_query) | errors: []}
errors = errors =
error error
@ -1107,7 +1116,7 @@ defmodule AshPhoenix.Form do
if opts[:raise?] do if opts[:raise?] do
raise Ash.Error.to_error_class(changeset.errors, changeset: changeset) raise Ash.Error.to_error_class(changeset.errors, changeset: changeset)
else else
changeset = %{changeset | errors: []} changeset = %{(changeset || original_changeset_or_query) | errors: []}
errors = errors =
error error
@ -1145,6 +1154,10 @@ defmodule AshPhoenix.Form do
end end
end end
defp with_changeset(changeset, func) do
{changeset, func.(changeset)}
end
@doc """ @doc """
Same as `submit/2`, but raises an error if the submission fails. Same as `submit/2`, but raises an error if the submission fails.
""" """

View file

@ -72,7 +72,7 @@ defmodule AshPhoenix.MixProject do
# Run "mix help deps" to learn about dependencies. # Run "mix help deps" to learn about dependencies.
defp deps do 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, "~> 1.5.6 or ~> 1.6.0"},
{:phoenix_html, "~> 2.14 or ~> 3.0"}, {:phoenix_html, "~> 2.14 or ~> 3.0"},
{:phoenix_live_view, "~> 0.15"}, {:phoenix_live_view, "~> 0.15"},

View file

@ -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"}, "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"}, "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},

View file

@ -548,8 +548,8 @@ defmodule AshPhoenix.FormTest do
end end
test "it raises an appropriate error when the incorrect api is configured" do test "it raises an appropriate error when the incorrect api is configured" do
assert_raise RuntimeError, assert_raise Ash.Error.Invalid.NoSuchResource,
~r/Resource AshPhoenix.Test.Post not found in api AshPhoenix.Test.OtherApi/, ~r/No such resource AshPhoenix.Test.Post/,
fn -> fn ->
Post Post
|> Form.for_create(:create, api: OtherApi) |> Form.for_create(:create, api: OtherApi)

View file

@ -1,7 +1,4 @@
defmodule AshPhoenix.Test.OtherApi do defmodule AshPhoenix.Test.OtherApi do
@moduledoc false @moduledoc false
use Ash.Api use Ash.Api
resources do
end
end end