defmodule MvWeb.Helpers.AshErrorHelpers do @moduledoc """ Shared formatting for Ash errors surfaced as flash messages in the member show LiveComponents. Centralizes the translation of `Ash.Error.Invalid` / `Ash.Error.Forbidden` (and plain string/unknown errors) into user-facing text so the components do not each carry their own copy. """ use Gettext, backend: MvWeb.Gettext @doc """ Turns an Ash error into a human-readable, localized string. - `Ash.Error.Invalid` — joins the individual error messages, falling back to `inspect/1` for sub-errors that carry no `:message`. - `Ash.Error.Forbidden` — a localized "not allowed" message. - a binary — passed through unchanged (already a ready-to-show message). - anything else — a localized generic error message. """ def format_error(%Ash.Error.Invalid{errors: errors}) do Enum.map_join(errors, ", ", fn %{message: message} -> message other -> inspect(other) end) end def format_error(%Ash.Error.Forbidden{}) do gettext("You are not allowed to perform this action.") end def format_error(error) when is_binary(error), do: error def format_error(_error), do: gettext("An error occurred") end