Skip to content

Commit

Permalink
Fix error handling for query cast errors (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Apr 22, 2024
1 parent ff0f0fd commit a5b4fc8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/console/graphql/exceptions/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ defimpl Console.GraphQl.Exception, for: Ecto.NoResultsError do
end

defimpl Console.GraphQl.Exception, for: Ecto.CastError do
def error(_), do: {400, "invalid input"}
def error(_), do: {400, "could not find resource"}
end

defimpl Console.GraphQl.Exception, for: Ecto.Query.CastError do
def error(_), do: {404, "could not find resource"}
end
8 changes: 5 additions & 3 deletions lib/console/graphql/middleware/safe_resolution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ defmodule Console.Middleware.SafeResolution do
Resolution.call(resolution, resolver)
rescue
exception ->
{_, msg} = Console.GraphQl.Exception.error(exception)
error = Exception.format(:error, exception, __STACKTRACE__)
Logger.error(error)
{code, msg} = Console.GraphQl.Exception.error(exception)
if code >= 500 do
error = Exception.format(:error, exception, __STACKTRACE__)
Logger.error(error)
end
Resolution.put_result(resolution, {:error, msg})
end
end

0 comments on commit a5b4fc8

Please sign in to comment.