diff --git a/lib/mv_web/live/custom_field_value_live/index.ex b/lib/mv_web/live/custom_field_value_live/index.ex index 0847cd6..eea578e 100644 --- a/lib/mv_web/live/custom_field_value_live/index.ex +++ b/lib/mv_web/live/custom_field_value_live/index.ex @@ -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