refactor(import): drop unreachable CSV error-formatting path

consume_and_read_csv/2 and MemberCSV.prepare/2 only ever return {:error, binary()}, so the non-binary error branch and the format_error_message/* helpers it called were unreachable. Removed them and bound the remaining discarded locale/dispatch results.
This commit is contained in:
Moritz 2026-06-02 11:50:43 +02:00
parent c0395f16e8
commit d9a5a081df

View file

@ -47,14 +47,11 @@ defmodule MvWeb.ImportLive do
# after this limit is reached. # after this limit is reached.
@max_errors 50 @max_errors 50
# Maximum length for error messages before truncation
@max_error_message_length 200
@impl true @impl true
def mount(_params, session, socket) do def mount(_params, session, socket) do
# Get locale from session for translations # Get locale from session for translations
locale = session["locale"] || "de" locale = session["locale"] || "de"
Gettext.put_locale(MvWeb.Gettext, locale) _ = Gettext.put_locale(MvWeb.Gettext, locale)
# Get club name from settings # Get club name from settings
club_name = club_name =
@ -193,16 +190,6 @@ defmodule MvWeb.ImportLive do
:error, :error,
gettext("Failed to prepare CSV import: %{reason}", reason: reason) gettext("Failed to prepare CSV import: %{reason}", reason: reason)
)} )}
{:error, error} ->
error_message = format_error_message(error)
{:noreply,
put_flash(
socket,
:error,
gettext("Failed to prepare CSV import: %{reason}", reason: error_message)
)}
end end
end end
@ -223,64 +210,6 @@ defmodule MvWeb.ImportLive do
{:noreply, socket} {:noreply, socket}
end end
# Formats error messages for user-friendly display.
#
# Handles various error types including Ash errors, maps with message fields,
# lists of errors, and fallback formatting for unknown types.
@spec format_error_message(any()) :: String.t()
defp format_error_message(error) do
case error do
%Ash.Error.Invalid{} = ash_error ->
format_ash_error(ash_error)
%{message: msg} when is_binary(msg) ->
msg
%{errors: errors} when is_list(errors) ->
format_error_list(errors)
reason when is_binary(reason) ->
reason
other ->
format_unknown_error(other)
end
end
# Formats Ash validation errors for display
defp format_ash_error(%Ash.Error.Invalid{errors: errors}) when is_list(errors) do
Enum.map_join(errors, ", ", &format_single_error/1)
end
defp format_ash_error(error) do
format_unknown_error(error)
end
# Formats a list of errors into a readable string
defp format_error_list(errors) do
Enum.map_join(errors, ", ", &format_single_error/1)
end
# Formats a single error item
defp format_single_error(error) when is_map(error) do
Map.get(error, :message) || Map.get(error, :field) || inspect(error, limit: :infinity)
end
defp format_single_error(error) do
to_string(error)
end
# Formats unknown error types with truncation for very long messages
defp format_unknown_error(other) do
error_str = inspect(other, limit: :infinity, pretty: true)
if String.length(error_str) > @max_error_message_length do
String.slice(error_str, 0, @max_error_message_length - 3) <> "..."
else
error_str
end
end
@impl true @impl true
def handle_info({:process_chunk, idx}, socket) do def handle_info({:process_chunk, idx}, socket) do
case socket.assigns do case socket.assigns do
@ -337,32 +266,33 @@ defmodule MvWeb.ImportLive do
actor: actor actor: actor
] ]
if Config.sql_sandbox?() do _ =
run_chunk_with_locale( if Config.sql_sandbox?() do
locale, run_chunk_with_locale(
chunk, locale,
import_state.column_map, chunk,
import_state.custom_field_map, import_state.column_map,
opts, import_state.custom_field_map,
live_view_pid, opts,
idx live_view_pid,
) idx
else )
Task.Supervisor.start_child( else
Mv.TaskSupervisor, Task.Supervisor.start_child(
fn -> Mv.TaskSupervisor,
run_chunk_with_locale( fn ->
locale, run_chunk_with_locale(
chunk, locale,
import_state.column_map, chunk,
import_state.custom_field_map, import_state.column_map,
opts, import_state.custom_field_map,
live_view_pid, opts,
idx live_view_pid,
) idx
end )
) end
end )
end
{:noreply, socket} {:noreply, socket}
end end
@ -378,7 +308,7 @@ defmodule MvWeb.ImportLive do
live_view_pid, live_view_pid,
idx idx
) do ) do
Gettext.put_locale(MvWeb.Gettext, locale) _ = Gettext.put_locale(MvWeb.Gettext, locale)
ImportRunner.process_chunk(chunk, column_map, custom_field_map, opts, live_view_pid, idx) ImportRunner.process_chunk(chunk, column_map, custom_field_map, opts, live_view_pid, idx)
end end