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:
parent
5fe46ebe64
commit
fda2d150db
2 changed files with 20 additions and 8 deletions
|
@ -194,7 +194,13 @@ parse_qs(#{qs := Qs}) ->
|
||||||
|
|
||||||
-spec match_qs(cowboy:fields(), req()) -> map().
|
-spec match_qs(cowboy:fields(), req()) -> map().
|
||||||
match_qs(Fields, Req) ->
|
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().
|
-spec uri(req()) -> iodata().
|
||||||
uri(Req) ->
|
uri(Req) ->
|
||||||
|
@ -407,7 +413,13 @@ parse_cookies(Req) ->
|
||||||
|
|
||||||
-spec match_cookies(cowboy:fields(), req()) -> map().
|
-spec match_cookies(cowboy:fields(), req()) -> map().
|
||||||
match_cookies(Fields, Req) ->
|
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.
|
%% Request body.
|
||||||
|
|
||||||
|
@ -803,12 +815,7 @@ kvlist_to_map(Keys, [{Key, Value}|Tail], Map) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
filter(Fields, Map0) ->
|
filter(Fields, Map0) ->
|
||||||
case filter(Fields, Map0, #{}) of
|
filter(Fields, Map0, #{}).
|
||||||
{ok, Map} ->
|
|
||||||
Map;
|
|
||||||
{error, Errors} ->
|
|
||||||
exit({validation_failed, Errors})
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% Loop through fields, if value is missing and no default,
|
%% Loop through fields, if value is missing and no default,
|
||||||
%% record the error; else if value is missing and has a
|
%% record the error; else if value is missing and has a
|
||||||
|
|
|
@ -171,6 +171,9 @@ match_cookies(Config) ->
|
||||||
<<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config),
|
<<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config),
|
||||||
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/cookies/a/c",
|
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/cookies/a/c",
|
||||||
[{<<"cookie">>, "a=b; c=d"}], Config),
|
[{<<"cookie">>, "a=b; c=d"}], Config),
|
||||||
|
%% Ensure match errors result in a 400 response.
|
||||||
|
{400, _, _} = do_get("/match/cookies/a/c",
|
||||||
|
[{<<"cookie">>, "a=b"}], Config),
|
||||||
%% This function is tested more extensively through unit tests.
|
%% This function is tested more extensively through unit tests.
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
@ -183,6 +186,8 @@ match_qs(Config) ->
|
||||||
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a=b&c=d", Config),
|
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a=b&c=d", Config),
|
||||||
<<"#{a => <<\"b\">>,c => true}">> = do_get_body("/match/qs/a/c?a=b&c", Config),
|
<<"#{a => <<\"b\">>,c => true}">> = do_get_body("/match/qs/a/c?a=b&c", Config),
|
||||||
<<"#{a => true,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a&c=d", Config),
|
<<"#{a => true,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a&c=d", Config),
|
||||||
|
%% Ensure match errors result in a 400 response.
|
||||||
|
{400, _, _} = do_get("/match/qs/a/c?a=b", [], Config),
|
||||||
%% This function is tested more extensively through unit tests.
|
%% This function is tested more extensively through unit tests.
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue