Skip to content

Instantly share code, notes, and snippets.

🌟
🚀

Sultan Iman imanhodjaev

🌟
🚀
  • Berlin
Block or report user

Report or block imanhodjaev

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View process_errors_helper.ex
def process_errors(%Changeset{} = changeset) do
%{
message: "Changeset errors occurred",
code: :schema_errors,
errors: to_api_errors(changeset)
}
end
View use-error-handling-middleware.ex
# add this to your schema module
# if it's a field for the mutation object, add this middleware to the end
def middleware(middleware, _field, %{identifier: :mutation}) do
middleware ++ [Idp.Middlewares.HandleAPIErrors]
end
# if it's any other object keep things as is
def middleware(middleware, _field, _object), do: middleware
@imanhodjaev
imanhodjaev / absinthe-middleware-handle-errors.ex
Last active May 7, 2019
Absinthe middleware handle to handle errors
View absinthe-middleware-handle-errors.ex
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)
View absinthe-resolver-action-wrapper.ex
def action_wrapped(fun) do
case fun.() do
{:ok, result} ->
{:ok, result}
{:error, changeset = %Changeset{}} ->
{
:error,
%{
message: "Changeset errors occurred",
@imanhodjaev
imanhodjaev / absinthe-resolver-with-standard-errors.ex
Last active Apr 28, 2019
Sample resolver with standard errors
View absinthe-resolver-with-standard-errors.ex
defmodule IdpWeb.Schema.CityResolvers do
use IdpWeb.Schema.Errors
alias Idp.Geo.Cities
alias Idp.EctoHelpers
def create(_parent, city, _ctx) do
EctoHelpers.action_wrapped(fn ->
Cities.create_city(city)
end)
View standard-absinthe-errors.ex
defmodule IdpWeb.Schema.Errors do
@moduledoc """
Defines common GraphQL error responses
"""
defmacro __using__(_) do
quote do
@permission_denied {
:error,
%{
message: "Permission denied",
View ecto-helpers.ex
defmodule Idp.EctoHelpers do
alias Ecto.Changeset
@doc """
Transform `%Ecto.Changeset{}` errors to a map
containing field name as a key on which validation
error happened and it's formatted message.
For example:
View common-changeset-error.ex
{
:error,
%{
message: "Changeset errors occurred",
code: :changeset_errors,
errors: [
%{field: "error message"}
]
}
}
View common-absinthe-error-shape.ex
@permission_denied {
:error,
%{
message: gettext("Permission denied"),
code: :permission_denied
}
}
@imanhodjaev
imanhodjaev / declarative-schemas.py
Created Nov 29, 2018
Just sketching for declarative schemas
View declarative-schemas.py
def add(field_name, type, **options):
pass
def create_index(field_name):
pass
def schema(table_name, *actions):
return DatabaseTable(table_name)
You can’t perform that action at this time.