mobilizon/lib/graphql/helpers/error.ex
Thomas Citharel c56e299a09
Validate number of places being a positive integer
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2020-06-05 11:38:49 +02:00

46 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