Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/minirest_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,21 @@ apply_callback(Request, Params, Handler) ->
end.

%% response error
reply({ErrorStatus, #{code := Code, message := Message}}, Req, Handler)
reply({ErrorStatus, #{code := Code, message := Message} = Resp}, Req, Handler)
when (ErrorStatus < 200 orelse 300 =< ErrorStatus)
andalso is_atom(Code) ->
reply({ErrorStatus, Code, Message}, Req, Handler);
reply({ErrorStatus, Code, Message}, Req, Handler = #handler{error_codes = Codes})
Other = maps:without([code, message], Resp),
reply({ErrorStatus, Code, Message, Other}, Req, Handler);
reply({ErrorStatus, Code, Message}, Req, Handler)
when (ErrorStatus < 200 orelse 300 =< ErrorStatus)
andalso is_atom(Code) ->
reply({ErrorStatus, Code, Message, _Other = #{}}, Req, Handler);
reply({ErrorStatus, Code, Message, Other}, Req, Handler = #handler{error_codes = Codes})
when (ErrorStatus < 200 orelse 300 =< ErrorStatus)
andalso is_atom(Code) andalso is_map(Other) ->
case maybe_ignore_code_check(ErrorStatus, Code) orelse lists:member(Code, Codes) of
true ->
ErrorMessageStruct = {message, #{code => Code, message => Message}},
ErrorMessageStruct = {message, Other#{code => Code, message => Message}},
{ok, Headers, Body} = minirest_body:encode(ErrorMessageStruct),
reply({ErrorStatus, Headers, Body}, Req, Handler);
false ->
Expand Down
13 changes: 9 additions & 4 deletions test/minirest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
all() ->
[
t_lazy_body,
t_binary_body
t_binary_body,
t_flex_error
].

init_per_suite(Config) ->
Expand Down Expand Up @@ -61,8 +62,12 @@ t_binary_body(_Config) ->
{ok, {{_Version, 200, _Status}, _Headers, "alldataatonce"}},
httpc:request(address() ++ "/binary_body")).

t_flex_error(_Config) ->
{ok, {{_Version, 400, _Status}, _Headers, Body}} =
httpc:request(address() ++ "/flex_error"),
?assertMatch(
#{<<"code">> := _, <<"message">> := _, <<"hint">> := _},
jsx:decode(iolist_to_binary(Body), [return_maps])).

address() ->
"http://localhost:" ++ integer_to_list(?PORT).



21 changes: 19 additions & 2 deletions test/minirest_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
-export([api_spec/0]).

-export([lazy_body/2,
binary_body/2]).
binary_body/2,
flex_error/2]).

api_spec() ->
{
[lazy_body(),
binary_body()],
binary_body(),
flex_error()],
[]
}.

Expand Down Expand Up @@ -55,6 +57,19 @@ binary_body() ->
},
{"/binary_body", MetaData, binary_body}.

flex_error() ->
MetaData = #{
get => #{
description => "binary body",
responses => #{
<<"400">> => #{
content => #{
'application/json' => #{
schema => #{
type => string}}}}}}
},
{"/flex_error", MetaData, flex_error}.

lazy_body(get, _) ->
BodyQH = qlc:table(fun() -> [<<"first">>, <<"second">>] end, []),
{200, #{<<"content-type">> => <<"test/plain">>}, BodyQH}.
Expand All @@ -63,3 +78,5 @@ binary_body(get, _) ->
Body = <<"alldataatonce">>,
{200, #{<<"content-type">> => <<"test/plain">>}, Body}.

flex_error(get, _) ->
{400, #{message => <<"boom">>, code => 'BAD_REQUEST', hint => <<"something went wrong">>}}.