mirror of
https://framagit.org/framasoft/mobilizon.git
synced 2024-12-23 16:33:28 +00:00
c56e299a09
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
45 lines
1 KiB
Elixir
45 lines
1 KiB
Elixir
defmodule Mobilizon.GraphQL.Helpers.Error do
|
|
@moduledoc """
|
|
Helper functions for Mobilizon.GraphQL
|
|
"""
|
|
alias Ecto.Changeset
|
|
|
|
def handle_errors(fun) do
|
|
fn source, args, info ->
|
|
case Absinthe.Resolution.call(fun, source, args, info) do
|
|
{:error, %Changeset{} = changeset} ->
|
|
format_changeset(changeset)
|
|
|
|
{:error, _, %Changeset{} = changeset} ->
|
|
format_changeset(changeset)
|
|
|
|
val ->
|
|
val
|
|
end
|
|
end
|
|
end
|
|
|
|
def format_changeset(%Changeset{changes: changes} = changeset) do
|
|
# {:error, [email: {"has already been taken", []}]}
|
|
|
|
errors =
|
|
Enum.reduce(changes, [], fn {_key, value}, acc ->
|
|
case value do
|
|
%Changeset{} ->
|
|
{:error, errors} = format_changeset(value)
|
|
acc ++ errors
|
|
|
|
_ ->
|
|
acc
|
|
end
|
|
end)
|
|
|
|
errors = errors ++ Enum.map(changeset.errors, &transform_error/1)
|
|
|
|
{:error, errors}
|
|
end
|
|
|
|
defp transform_error({key, {value, _context}}) do
|
|
[message: "#{value}", details: key]
|
|
end
|
|
end
|