refactor(web): extract format_ash_error to MvWeb.ErrorHelpers

Use shared ErrorHelpers in UserLive.Index for consistent Ash error formatting.
This commit is contained in:
Moritz 2026-01-27 17:37:23 +01:00 committed by moritz
parent eb8d78f834
commit 41bc031cc6
2 changed files with 28 additions and 13 deletions

View file

@ -0,0 +1,25 @@
defmodule MvWeb.ErrorHelpers do
@moduledoc """
Shared helpers for formatting errors in the web layer.
Use `format_ash_error/1` for Ash errors so behaviour stays consistent
(e.g. handling Invalid errors whose entries may lack a `:message` field).
"""
@doc """
Formats an Ash error for display to the user.
Handles `Ash.Error.Invalid` by joining error messages; entries without
a `:message` field are inspected to avoid FunctionClauseError.
Other errors are inspected.
"""
@spec format_ash_error(Ash.Error.t() | term()) :: String.t()
def format_ash_error(%Ash.Error.Invalid{errors: errors}) when is_list(errors) do
Enum.map_join(errors, ", ", fn
%{message: message} when is_binary(message) -> message
other -> inspect(other)
end)
end
def format_ash_error(error), do: inspect(error)
end