Skip to content

Instantly share code, notes, and snippets.

@sultaniman
Last active May 7, 2019 21:31
Show Gist options
  • Save sultaniman/4dbf6f9da6fa1a2800a7a7030647b831 to your computer and use it in GitHub Desktop.
Save sultaniman/4dbf6f9da6fa1a2800a7a7030647b831 to your computer and use it in GitHub Desktop.
Absinthe middleware handle to handle errors
defmodule Idp.Middlewares.HandleAPIErrors do
@behaviour Absinthe.Middleware
alias Idp.EctoHelpers
def call(resolution, _config) do
errors =
resolution
|> Map.get(:errors)
|> Enum.flat_map(&process_errors/1)
%{resolution | errors: errors}
end
defp process_errors(%Ecto.Changeset{} = changeset) do
EctoHelpers.process_errors(changeset)
end
defp process_errors(error), do: [error]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment