fix: Replace Ash.read! with error handling in CustomFieldValueLive.Index

- Replace Ash.read! with Ash.read and proper error handling in mount/3
This commit is contained in:
Moritz 2026-01-13 14:57:39 +01:00
parent 807e03d86b
commit fba0ea5ec0
Signed by: moritz
GPG key ID: 1020A035E5DD0824

View file

@ -75,10 +75,35 @@ defmodule MvWeb.CustomFieldValueLive.Index do
def mount(_params, _session, socket) do
actor = current_actor(socket)
{:ok,
socket
|> assign(:page_title, "Listing Custom field values")
|> stream(:custom_field_values, Ash.read!(Mv.Membership.CustomFieldValue, actor: actor))}
# Early return if no actor (prevents exceptions in unauthenticated tests)
if is_nil(actor) do
{:ok,
socket
|> assign(:page_title, "Listing Custom field values")
|> stream(:custom_field_values, [])}
else
case Ash.read(Mv.Membership.CustomFieldValue, actor: actor) do
{:ok, custom_field_values} ->
{:ok,
socket
|> assign(:page_title, "Listing Custom field values")
|> stream(:custom_field_values, custom_field_values)}
{:error, %Ash.Error.Forbidden{}} ->
{:ok,
socket
|> assign(:page_title, "Listing Custom field values")
|> stream(:custom_field_values, [])
|> put_flash(:error, gettext("You do not have permission to view custom field values"))}
{:error, error} ->
{:ok,
socket
|> assign(:page_title, "Listing Custom field values")
|> stream(:custom_field_values, [])
|> put_flash(:error, format_error(error))}
end
end
end
@impl true