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:
parent
eb8d78f834
commit
41bc031cc6
2 changed files with 28 additions and 13 deletions
25
lib/mv_web/error_helpers.ex
Normal file
25
lib/mv_web/error_helpers.ex
Normal 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
|
||||||
|
|
@ -26,6 +26,7 @@ defmodule MvWeb.UserLive.Index do
|
||||||
import MvWeb.LiveHelpers, only: [current_actor: 1]
|
import MvWeb.LiveHelpers, only: [current_actor: 1]
|
||||||
|
|
||||||
require Ash.Query
|
require Ash.Query
|
||||||
|
import MvWeb.ErrorHelpers, only: [format_ash_error: 1]
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, _session, socket) do
|
||||||
|
|
@ -71,7 +72,7 @@ defmodule MvWeb.UserLive.Index do
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{:error, error} ->
|
{:error, error} ->
|
||||||
{:noreply, put_flash(socket, :error, format_error(error))}
|
{:noreply, put_flash(socket, :error, format_ash_error(error))}
|
||||||
end
|
end
|
||||||
|
|
||||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||||
|
|
@ -82,7 +83,7 @@ defmodule MvWeb.UserLive.Index do
|
||||||
put_flash(socket, :error, gettext("You do not have permission to access this user"))}
|
put_flash(socket, :error, gettext("You do not have permission to access this user"))}
|
||||||
|
|
||||||
{:error, error} ->
|
{:error, error} ->
|
||||||
{:noreply, put_flash(socket, :error, format_error(error))}
|
{:noreply, put_flash(socket, :error, format_ash_error(error))}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -144,15 +145,4 @@ defmodule MvWeb.UserLive.Index do
|
||||||
defp toggle_order(:desc), do: :asc
|
defp toggle_order(:desc), do: :asc
|
||||||
defp sort_fun(:asc), do: &<=/2
|
defp sort_fun(:asc), do: &<=/2
|
||||||
defp sort_fun(:desc), do: &>=/2
|
defp sort_fun(:desc), do: &>=/2
|
||||||
|
|
||||||
defp format_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
|
|
||||||
|
|
||||||
defp format_error(error) do
|
|
||||||
inspect(error)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue