Skip to content

Commit

Permalink
Catch any unexpected graphql internal crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Dec 16, 2021
1 parent 8cde3a8 commit c2f50e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ execute(Ep, #{document := Doc,
{ok, graphql:execute(Ep, Ctx2, Ast2)}
catch
throw:{error, Err} ->
{error, Err}
{error, Err};
Class:Reason:Stacktrace ->
Err = #{what => graphql_internal_crash,
class => Class, reason => Reason,
stacktrace => Stacktrace},
?LOG_ERROR(Err),
{error, internal_crash}
end.

%% @doc Execute selected operation on a given endpoint with authorization.
Expand Down
12 changes: 11 additions & 1 deletion src/mongoose_graphql/mongoose_graphql_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@

-ignore_xref([format_error/1, err/2, crash/2]).

-type err_msg() :: #{message := binary(), extensions => map(), path => list()}.

%% callback invoked when resolver returns error tuple
-spec err(map(), term()) -> err_msg().
err(_Ctx, domain_not_found) ->
#{message => <<"Given domain does not exist">>, extensions => #{code => resolver_error}};
err(_Ctx, ErrorTerm) ->
#{message => iolist_to_binary(io_lib:format("~p", [ErrorTerm])),
extensions => #{code => resolver_error}}.

%% callback invoked when resolver crashes
-spec crash(map(), term()) -> err_msg().
crash(_Ctx, #{type := Type}) ->
#{message => <<"Unexpected ", Type/binary, " resolver crash">>,
extensions => #{code => resolver_crash}}.

%% @doc Format error that occurred in any phase including HTTP request decoding.
-spec format_error(term())-> {integer(), err_msg()}.
format_error(#{phase := Phase, error_term := Term}) when Phase =:= authorize;
Phase =:= decode;
Phase =:= parse ->
Expand All @@ -28,7 +33,12 @@ format_error(#{error_term := _, phase := Phase} = Err) when Phase =:= execute;
Phase =:= type_check;
Phase =:= validate;
Phase =:= uncategorized ->
{400, graphql:format_errors(#{}, [Err])};
[ErrMsg] = graphql:format_errors(#{}, [Err]),
{400, ErrMsg};
format_error(internal_crash) ->
Msg = #{message => <<"GraphQL Internal Server Error">>,
extensions => #{code => internal_server_error}},
{500, Msg};
format_error(Err) ->
Msg = #{extensions => #{code => uncathegorized},
message => iolist_to_binary(io_lib:format("~p", [Err]))},
Expand Down

0 comments on commit c2f50e4

Please sign in to comment.