Crudry v2.4.0 Crudry.Middlewares.TranslateErrors View Source

Absinthe Middleware to translate errors and changeset errors into human readable messages. It support nested changeset errors and internationalization, using Gettext.

Usage

Cudry.Translator is used by default to translate error messages to the default locale en. You can also use your own Gettext module by adding it to your Absinthe's schema context/1 function:

def context(context) do
  Map.put(context, :translator, MyAppWeb.Gettext)
end

Or just override the default locale in your Context Plug:

def call(conn, _) do
  Absinthe.Plug.put_options(conn, context: %{locale: "pt_BR"})
end

Then, to handle errors for a field, add it after the resolve, using middleware/2:

alias Crudry.Middlewares.TranslateErrors

field :create_user, :user do
  arg :params, non_null(:user_params)

  resolve &UsersResolver.create_user/2
  middleware TranslateErrors
end

To handle errors for all fields, use middleware/3:

alias Crudry.Middlewares.TranslateErrors

def middleware(middleware, _field, _object) do
  middleware ++ [TranslateErrors]
end

Examples

For a simple changeset error:

#Ecto.Changeset<
  action: nil,
  changes: %{},
  errors: [username: {"can't be blank", [validation: :required]}],
  data: #Crudry.User<>,
  valid?: false
>

The resulting error will be ["username can't be blank"]

For a changeset with nested errors:

#Ecto.Changeset<
  action: nil,
  changes: %{
    posts: [
      #Ecto.Changeset<
        action: :insert,
        changes: %{},
        errors: [
          title: {"can't be blank", [validation: :required]},
          user_id: {"can't be blank", [validation: :required]}
        ],
        data: #Crudry.Post<>,
        valid?: false
      >
    ]
  },
  errors: [
    username: {"should be at least %{count} character(s)",
    [count: 2, validation: :length, kind: :min]}
  ],
  data: #Crudry.User<>,
  valid?: false
>

The resulting error will be ["posts: title can't be blank", "posts: user_id can't be blank", "username should be at least 2 character(s)"]

Link to this section Summary

Link to this section Functions

Link to this function

call(resolution, config)

View Source

Callback implementation for Absinthe.Middleware.call/2.

Link to this function

handle_error(changeset, translator, locale)

View Source