Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom message in individual policy #1543

Open
yujonglee opened this issue Oct 22, 2024 · 2 comments
Open

Custom message in individual policy #1543

yujonglee opened this issue Oct 22, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@yujonglee
Copy link

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
Copy link
Author

hi @zachdaniel, is there some kind of workaround for this? (ways to know which policy cause Forbidden error from the caller side)

@zachdaniel
Copy link
Contributor

Not so much right now. Your best bet would be to define a calculation that determines if a user's plan is sufficient, and share that between the policy and the UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Someday
Development

No branches or pull requests

2 participants