2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Helpers.Error do
|
2019-02-07 09:27:54 +00:00
|
|
|
@moduledoc """
|
2020-01-26 19:34:25 +00:00
|
|
|
Helper functions for Mobilizon.GraphQL
|
2019-02-07 09:27:54 +00:00
|
|
|
"""
|
|
|
|
|
2019-01-25 14:41:10 +00:00
|
|
|
def handle_errors(fun) do
|
|
|
|
fn source, args, info ->
|
|
|
|
case Absinthe.Resolution.call(fun, source, args, info) do
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} -> format_changeset(changeset)
|
|
|
|
val -> val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_changeset(changeset) do
|
|
|
|
# {:error, [email: {"has already been taken", []}]}
|
|
|
|
errors =
|
|
|
|
changeset.errors
|
2019-01-29 10:02:32 +00:00
|
|
|
|> Enum.map(fn {key, {value, _context}} ->
|
|
|
|
[message: "#{value}", details: key]
|
2019-01-25 14:41:10 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
{:error, errors}
|
|
|
|
end
|
|
|
|
end
|