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
@imanhodjaev
imanhodjaev / dynamic-supervisor-via-registry.ex
Created Apr 30, 2020
How to create dynamic supervisor with registry
View dynamic-supervisor-via-registry.ex
# Your actual dynamic worker
defmodule Awesome.Worker do
use GenServer, restart: :transient
alias Awesome.Hero
# Client
def start_link(%Hero{} = hero), do:
GenServer.start_link(@mod, hero, name: proc_name(hero))
defp proc_name(%Hero{name: name}), do:
View cran-package-metadata-parser.py
def from_packages_list(data: str) -> Generator:
"""Parses CRAN package metadata from
https://cran.r-project.org/src/contrib/PACKAGES
and returns the list of dictionaries.
Args:
data (str): raw text from the package list
Returns:
(Generator): each entry from packages as dictionary
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"}
]
}
}
You can’t perform that action at this time.