ash icon indicating copy to clipboard operation
ash copied to clipboard

Custom message in individual policy

Open yujonglee opened this issue 4 months ago • 2 comments

Is your feature request related to a problem? Please describe.

I have this code:

  @impl true
  def handle_event("submit", %{"form" => params}, socket) do
    case AshPhoenix.Form.submit(socket.assigns.form, params: params) do
      {:ok, %{id: id}} ->
        {:noreply, socket |> push_navigate(to: ~p"/source/#{id}")}

      {:error,
       %Phoenix.HTML.Form{source: %AshPhoenix.Form{source: %Ash.Changeset{errors: errors}}} = form} ->
        if Enum.any?(errors, &match?(%Ash.Error.Forbidden.Policy{}, &1)) do
          socket =
            socket
            |> push_navigate(to: ~p"/source")
            |> put_flash(:error, "Please upgrade your plan.")

          {:noreply, socket}
        else
          {:noreply, socket |> assign(:form, form)}
        end
    end
  end

Ideally, I should not hard-code "Please upgrade your plan." message in the caller side, but in the policy side. So that caller can just put_flash the error message coming from the policy.

Describe the solution you'd like

Able to set some value from the policy, and that is returned to caller as some property of Ash.Error.Forbidden.Policy.

Describe alternatives you've considered

Validation.

Express the feature either with a change to resource syntax, or with a change to the resource interface

Additional context

yujonglee avatar Oct 22 '24 00:10 yujonglee