mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 05:13:33 +12:00
improvement: add AshGraphql.Errors.to_errors/3
This commit is contained in:
parent
7bb2387fb8
commit
913afc6d58
2 changed files with 52 additions and 41 deletions
48
lib/graphql/errors.ex
Normal file
48
lib/graphql/errors.ex
Normal file
|
@ -0,0 +1,48 @@
|
|||
defmodule AshGraphql.Errors do
|
||||
require Logger
|
||||
|
||||
@doc """
|
||||
Transform an error or list of errors into the response for graphql.
|
||||
"""
|
||||
def to_errors(errors, context, api) do
|
||||
errors
|
||||
|> AshGraphql.Graphql.Resolver.unwrap_errors()
|
||||
|> Enum.map(fn error ->
|
||||
if AshGraphql.Error.impl_for(error) do
|
||||
error = AshGraphql.Error.to_error(error)
|
||||
|
||||
case AshGraphql.Api.Info.error_handler(api) do
|
||||
nil ->
|
||||
error
|
||||
|
||||
{m, f, a} ->
|
||||
apply(m, f, [error, context | a])
|
||||
end
|
||||
else
|
||||
uuid = Ash.UUID.generate()
|
||||
|
||||
if is_exception(error) do
|
||||
case error do
|
||||
%{stacktrace: %{stacktrace: stacktrace}} ->
|
||||
Logger.warn(
|
||||
"`#{uuid}`: AshGraphql.Error not implemented for error:\n\n#{Exception.format(:error, error, stacktrace)}"
|
||||
)
|
||||
|
||||
error ->
|
||||
Logger.warn(
|
||||
"`#{uuid}`: AshGraphql.Error not implemented for error:\n\n#{Exception.format(:error, error)}"
|
||||
)
|
||||
end
|
||||
else
|
||||
Logger.warn(
|
||||
"`#{uuid}`: AshGraphql.Error not implemented for error:\n\n#{inspect(error)}"
|
||||
)
|
||||
end
|
||||
|
||||
%{
|
||||
message: "something went wrong. Unique error id: `#{uuid}`"
|
||||
}
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
|
@ -1263,9 +1263,10 @@ defmodule AshGraphql.Graphql.Resolver do
|
|||
end
|
||||
end
|
||||
|
||||
defp unwrap_errors([]), do: []
|
||||
@doc false
|
||||
def unwrap_errors([]), do: []
|
||||
|
||||
defp unwrap_errors(errors) do
|
||||
def unwrap_errors(errors) do
|
||||
errors
|
||||
|> List.wrap()
|
||||
|> Enum.flat_map(fn
|
||||
|
@ -1278,45 +1279,7 @@ defmodule AshGraphql.Graphql.Resolver do
|
|||
end
|
||||
|
||||
defp to_errors(errors, context, api) do
|
||||
errors
|
||||
|> unwrap_errors()
|
||||
|> Enum.map(fn error ->
|
||||
if AshGraphql.Error.impl_for(error) do
|
||||
error = AshGraphql.Error.to_error(error)
|
||||
|
||||
case AshGraphql.Api.Info.error_handler(api) do
|
||||
nil ->
|
||||
error
|
||||
|
||||
{m, f, a} ->
|
||||
apply(m, f, [error, context | a])
|
||||
end
|
||||
else
|
||||
uuid = Ash.UUID.generate()
|
||||
|
||||
if is_exception(error) do
|
||||
case error do
|
||||
%{stacktrace: %{stacktrace: stacktrace}} ->
|
||||
Logger.warn(
|
||||
"`#{uuid}`: AshGraphql.Error not implemented for error:\n\n#{Exception.format(:error, error, stacktrace)}"
|
||||
)
|
||||
|
||||
error ->
|
||||
Logger.warn(
|
||||
"`#{uuid}`: AshGraphql.Error not implemented for error:\n\n#{Exception.format(:error, error)}"
|
||||
)
|
||||
end
|
||||
else
|
||||
Logger.warn(
|
||||
"`#{uuid}`: AshGraphql.Error not implemented for error:\n\n#{inspect(error)}"
|
||||
)
|
||||
end
|
||||
|
||||
%{
|
||||
message: "something went wrong. Unique error id: `#{uuid}`"
|
||||
}
|
||||
end
|
||||
end)
|
||||
AshGraphql.Errors.to_errors(errors, context, api)
|
||||
end
|
||||
|
||||
def resolve_calculation(
|
||||
|
|
Loading…
Reference in a new issue