refactor(member): share Ash error formatting across member-show components

This commit is contained in:
Moritz 2026-06-08 12:48:22 +02:00
parent 24f67bea80
commit 856ea4279c
7 changed files with 105 additions and 36 deletions

View file

@ -0,0 +1,34 @@
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