2019-01-25 14:41:10 +00:00
|
|
|
defmodule MobilizonWeb.Schema.Utils do
|
2019-02-07 09:27:54 +00:00
|
|
|
@moduledoc """
|
|
|
|
Helper functions for MobilizonWeb.Schema
|
|
|
|
"""
|
|
|
|
|
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
|