From 264bdb3def7b4109dde2c5fb5f95d967a24161d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Thu, 16 Dec 2021 12:37:30 +0100 Subject: [PATCH] Move http code to format_error function --- .../mongoose_graphql_cowboy_handler.erl | 14 +++++------ .../mongoose_graphql_errors.erl | 25 +++++++++++-------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/mongoose_graphql/mongoose_graphql_cowboy_handler.erl b/src/mongoose_graphql/mongoose_graphql_cowboy_handler.erl index b883ce1c4a..280866249e 100644 --- a/src/mongoose_graphql/mongoose_graphql_cowboy_handler.erl +++ b/src/mongoose_graphql/mongoose_graphql_cowboy_handler.erl @@ -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) -> @@ -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. @@ -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)), @@ -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) -> @@ -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), diff --git a/src/mongoose_graphql/mongoose_graphql_errors.erl b/src/mongoose_graphql/mongoose_graphql_errors.erl index c331143be4..3f6f0c050b 100644 --- a/src/mongoose_graphql/mongoose_graphql_errors.erl +++ b/src/mongoose_graphql/mongoose_graphql_errors.erl @@ -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) -> @@ -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).