From 50f6a108b47bb1b017d9f0a9690708d1b4283aac Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 16 May 2022 16:09:38 -0400 Subject: [PATCH] fix: raise error on non-existant resource for api --- lib/ash_phoenix/form/form.ex | 25 +++++++++++++++++++------ mix.exs | 2 +- mix.lock | 2 +- test/form_test.exs | 4 ++-- test/support/other_api.ex | 3 --- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/ash_phoenix/form/form.ex b/lib/ash_phoenix/form/form.ex index bcefdd3..adb1fc4 100644 --- a/lib/ash_phoenix/form/form.ex +++ b/lib/ash_phoenix/form/form.ex @@ -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 @@ -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 @@ -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 @@ -1063,6 +1071,7 @@ defmodule AshPhoenix.Form do ) |> before_submit.() |> form.api.destroy() + |> with_changeset(&form.api.update/1) :read -> form.resource @@ -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 @@ -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 @@ -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 @@ -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. """ diff --git a/mix.exs b/mix.exs index 1bfb960..fee3416 100644 --- a/mix.exs +++ b/mix.exs @@ -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"}, diff --git a/mix.lock b/mix.lock index 6cc799e..2e8358f 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, diff --git a/test/form_test.exs b/test/form_test.exs index 9691769..2fdb621 100644 --- a/test/form_test.exs +++ b/test/form_test.exs @@ -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) diff --git a/test/support/other_api.ex b/test/support/other_api.ex index d1feaa6..885e9bf 100644 --- a/test/support/other_api.ex +++ b/test/support/other_api.ex @@ -1,7 +1,4 @@ defmodule AshPhoenix.Test.OtherApi do @moduledoc false use Ash.Api - - resources do - end end