ash
ash copied to clipboard
Custom message in individual policy
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
hi @zachdaniel, is there some kind of workaround for this? (ways to know which policy cause Forbidden error from the caller side)
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.