Skip to content

Commit

Permalink
Add handling for atoms as error messages (#12)
Browse files Browse the repository at this point in the history
Co-Authored-By: Samuel Garneau <[email protected]>
  • Loading branch information
ACBullen and garno authored May 22, 2020
1 parent 99fa39c commit 62d532c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/absinthe_error_payload/payload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ defmodule AbsintheErrorPayload.Payload do
{:error, %ValidationMessage{}}
{:error, [%ValidationMessage{},%ValidationMessage{}]}
{:error, "This is an error"}
{:error, :error_atom}
{:error, ["This is an error", "This is another error"]}
```
Expand All @@ -206,12 +207,13 @@ defmodule AbsintheErrorPayload.Payload do
@doc """
Convert resolution errors to a mutation payload
The build payload middleware will accept lists of `AbsintheErrorPayload.ValidationMessage` or string errors.
The build payload middleware will accept lists of `AbsintheErrorPayload.ValidationMessage`, atom, or string errors.
Valid formats are:
```
[%ValidationMessage{},%ValidationMessage{}]
"This is an error"
:error_atom
["This is an error", "This is another error"]
```
"""
Expand All @@ -236,6 +238,7 @@ defmodule AbsintheErrorPayload.Payload do
{:error, %ValidationMessage{}}
{:error, [%ValidationMessage{},%ValidationMessage{}]}
{:error, "This is an error"}
{:error, :error_atom}
{:error, ["This is an error", "This is another error"]}
```
Expand Down Expand Up @@ -273,6 +276,8 @@ defmodule AbsintheErrorPayload.Payload do
|> error_payload()
end

def convert_to_payload({:error, message}) when is_atom(message), do: convert_to_payload({:error, "#{message}"})

def convert_to_payload({:error, list}) when is_list(list), do: error_payload(list)

def convert_to_payload(%Ecto.Changeset{valid?: false} = changeset) do
Expand Down Expand Up @@ -355,6 +360,10 @@ defmodule AbsintheErrorPayload.Payload do
generic_validation_message(message)
end

defp prepare_message(message) when is_atom(message) do
generic_validation_message("#{message}")
end

defp prepare_message(message) do
raise ArgumentError, "Unexpected validation message: #{inspect(message)}"
end
Expand Down
19 changes: 19 additions & 0 deletions test/payload_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ defmodule AbsintheErrorPayload.PayloadTest do
assert expected == value
end

test "error tuple with an atom message" do
resolution = resolution({:error, :an_error})

result = build_payload(resolution, nil)

assert %{value: value} = result

expected = payload(false, [%ValidationMessage{code: :unknown, field: nil, key: nil, message: "an_error", options: [], template: "an_error"}])
assert expected == value
end

test "error changeset" do
changeset =
{%{}, %{title: :string, title_lang: :string}}
Expand Down Expand Up @@ -159,6 +170,14 @@ defmodule AbsintheErrorPayload.PayloadTest do
assert_error_payload([message], result)
end

test "error from resolution, atom message" do
resolution = resolution_error([:an_error])
result = build_payload(resolution, nil)

message = %ValidationMessage{code: :unknown, message: "an_error", template: "an_error"}
assert_error_payload([message], result)
end

test "error from resolution, string message list" do
resolution = resolution_error(["an error", "another error"])
result = build_payload(resolution, nil)
Expand Down

0 comments on commit 62d532c

Please sign in to comment.