Skip to content

Commit

Permalink
Move http code to format_error function
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Dec 16, 2021
1 parent 9a9ddf1 commit 264bdb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/mongoose_graphql/mongoose_graphql_cowboy_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ is_authorized(Req, State) ->
{true, Req, State2};
error ->
Msg = make_error(authorize, wrong_credentials),
reply_error(401, Msg, Req, State)
reply_error(Msg, Req, State)
end
catch
exit:Err ->
reply_error(400, make_error(authorize, Err), Req, State)
reply_error(make_error(authorize, Err), Req, State)
end.

resource_exists(#{method := <<"GET">>} = Req, State) ->
Expand All @@ -85,7 +85,7 @@ to_html(Req, #{index_location := {priv_file, App, FileLocation}} = State) ->
json_request(Req, State) ->
case gather(Req) of
{error, Reason} ->
reply_error(400, Reason, Req, State);
reply_error(Reason, Req, State);
{ok, Req2, Decoded} ->
run_request(Decoded, Req2, State)
end.
Expand Down Expand Up @@ -119,7 +119,7 @@ auth_admin(_, State) ->
{ok, State#{authorized => true}}.

run_request(#{document := undefined}, Req, State) ->
reply_error(400, make_error(decode, no_query_supplied), Req, State);
reply_error(make_error(decode, no_query_supplied), Req, State);
run_request(#{} = ReqCtx, Req, #{schema_endpoint := EpName,
authorized := AuthStatus} = State) ->
Ep = mongoose_graphql:get_endpoint(binary_to_existing_atom(EpName)),
Expand All @@ -132,7 +132,7 @@ run_request(#{} = ReqCtx, Req, #{schema_endpoint := EpName,
Reply = cowboy_req:reply(200, Req2),
{stop, Reply, State};
{error, Reason} ->
reply_error(400, Reason, Req, State)
reply_error(Reason, Req, State)
end.

gather(Req) ->
Expand Down Expand Up @@ -193,8 +193,8 @@ operation_name([]) ->
make_error(Phase, Term) ->
#{error_term => Term, phase => Phase}.

reply_error(Code, Msg, Req, State) ->
Error = mongoose_graphql_errors:format_error(Msg),
reply_error(Msg, Req, State) ->
{Code, Error} = mongoose_graphql_errors:format_error(Msg),
Body = jiffy:encode(#{errors => [Error]}),
Req2 = cowboy_req:set_resp_body(Body, Req),
Reply = cowboy_req:reply(Code, Req2),
Expand Down
25 changes: 14 additions & 11 deletions src/mongoose_graphql/mongoose_graphql_errors.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
%% @doc Implements callbacks that format custom errors returned from resolvers or crashes.
-module(mongoose_graphql_errors).

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

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

%% callback invoked when resolver returns error tuple
err(_Ctx, domain_not_found) ->
Expand All @@ -17,27 +17,30 @@ crash(_Ctx, #{type := Type}) ->
#{message => <<"Unexpected ", Type/binary, " resolver crash">>,
extensions => #{code => resolver_crash}}.

%% @doc Format errors that occured in any phase.
format_errors(Errors) ->
[format_error(E) || E <- Errors].

%% @doc Format error that occurred in any phase including HTTP request decoding.
format_error(#{phase := Phase, error_term := Term}) when Phase =:= authorize;
Phase =:= decode;
Phase =:= parse ->
#{extensions => #{code => err_code(Phase, Term)},
message => iolist_to_binary(err_msg(Phase, Term))};
Msg = #{extensions => #{code => err_code(Phase, Term)},
message => iolist_to_binary(err_msg(Phase, Term))},
{err_http_code(Phase), Msg};
format_error(#{error_term := _, phase := Phase} = Err) when Phase =:= execute;
Phase =:= type_check;
Phase =:= validate;
Phase =:= uncategorized ->
graphql:format_errors(#{}, [Err]);
{400, graphql:format_errors(#{}, [Err])};
format_error(Err) ->
#{extensions => #{code => uncathegorized},
message => iolist_to_binary(io_lib:format("~p", [Err]))}.
Msg = #{extensions => #{code => uncathegorized},
message => iolist_to_binary(io_lib:format("~p", [Err]))},
{400, Msg}.

%% Internal

err_http_code(authorize) ->
401;
err_http_code(_) ->
400.

err_code(_, Term) ->
simplify(Term).

Expand Down

0 comments on commit 264bdb3

Please sign in to comment.