DeliriumTremex is a library for standardized GraphQL error handling through Absinthe.
All errors should be returned it the errors
field.
Errors have the following format:
{
"key": "username",
"message": "Username is already taken",
"messages": ["is already taken", "is too short"],
"fullMessages": ["Username is already taken", "Username is too short"],
"index": null,
"subErrors": null
}
Field explantion:
key
- The key the error is attached tomessage
- A single error message associated to the keymessages
- List of all the non-formatted error messages associated with the keyfullMessages
- List of all the formatted error messages associated with the keyindex
- If the error is a nested error, specifies the index in the array at which the error occuredsubErrors
- Contains all sub-errors of the key
Sub-errors are errors that occur in nested data. E.g. let's say that you are
creating an article together with an array of comments. If any of those
comments fail validation their errors would be returned in the subErrors
array.
E.g.: If the second comment's content is too short.
{
"key": "comments",
"message": "Error validating all comments",
"messages": null,
"fullMessages": null,
"index": null,
"subErrors": [
{
"key": "content",
"message": "Content is too short",
"messages": ["is too short"],
"fullMessages": ["Content is too short"],
"index": 1,
"subErrors": null
}
]
}
Note that sub-errors can also have sub-errors which allows for basically infinite nesting. This should satisfy most use cases.
Currently DeliriumTremex
integrates with:
- Ecto - Automatically formats validation errors if passed a changeset.
Add the following to your mix.exs
file:
defp deps do
[
{:absinthe, "~> 1.4"},
{:delirium_tremex, "~> 1.0.0"}
]
end
If you have other dependencies just append the contents of the list above to your dependencies.
In your GraphQL/Absinthe schema add the following middleware to the queries/mutations for which you want the errors to be formatted.
e.g.
alias DeliriumTremex.Middleware.HandleErrors
query do
field :current_account, type: :account do
resolve &AccountResolver.current_account/2
middleware HandleErrors # <-- This line adds the error handeling
end
end
mutation do
field :register, type: :account do
arg :username, :string
arg :password, :string
resolve &AccountResolver.register/2
middleware HandleErrors # <-- This line adds the error handeling
end
end
In your config.exs you can specify an optional error builder file to handle specific error responses.
Unhandled error responses will be formatted as unknown errors. e.g.
%{
key: :unknown_error,
message: "Something went wrong",
messages: ["Something went wrong"]
}
Example for the config.exs:
config :delirium_tremex,
error_builder: YourAppName.ErrorBuilder
Example for the error_builder.ex:
defmodule YourAppName.ErrorBuilder do
def unauthorized do
%{
message: "You have insufficient privileges to access this resource",
messages: ["You have insufficient privileges to access this resource"]
}
end
end
Documentation available on https://hexdocs.pm/alcotest.
For suggestions, fixes or questions, please feel free to open an issue. Pull requests are always welcome.
This project is licensed under the MIT license. It comes with absolutely no warranty. The license is available in this repository.