0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Fix error response when constraint validation fails

This commit is contained in:
Loïc Hoguin 2017-09-27 16:20:38 +02:00
parent 5fe46ebe64
commit fda2d150db
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
2 changed files with 20 additions and 8 deletions

View file

@ -194,7 +194,13 @@ parse_qs(#{qs := Qs}) ->
-spec match_qs(cowboy:fields(), req()) -> map().
match_qs(Fields, Req) ->
filter(Fields, kvlist_to_map(Fields, parse_qs(Req))).
case filter(Fields, kvlist_to_map(Fields, parse_qs(Req))) of
{ok, Map} ->
Map;
{error, Errors} ->
exit({request_error, {match_qs, Errors},
'Query string validation constraints failed for the reasons provided.'})
end.
-spec uri(req()) -> iodata().
uri(Req) ->
@ -407,7 +413,13 @@ parse_cookies(Req) ->
-spec match_cookies(cowboy:fields(), req()) -> map().
match_cookies(Fields, Req) ->
filter(Fields, kvlist_to_map(Fields, parse_cookies(Req))).
case filter(Fields, kvlist_to_map(Fields, parse_cookies(Req))) of
{ok, Map} ->
Map;
{error, Errors} ->
exit({request_error, {match_cookies, Errors},
'Cookie validation constraints failed for the reasons provided.'})
end.
%% Request body.
@ -803,12 +815,7 @@ kvlist_to_map(Keys, [{Key, Value}|Tail], Map) ->
end.
filter(Fields, Map0) ->
case filter(Fields, Map0, #{}) of
{ok, Map} ->
Map;
{error, Errors} ->
exit({validation_failed, Errors})
end.
filter(Fields, Map0, #{}).
%% Loop through fields, if value is missing and no default,
%% record the error; else if value is missing and has a