diff options
author | Loïc Hoguin <[email protected]> | 2017-09-27 16:20:38 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2017-09-27 16:20:38 +0200 |
commit | fda2d150db460dfe66f307d976be021b7fa47722 (patch) | |
tree | 633b02663da15a7a7e5d8a19b6a4ddb545725963 /src | |
parent | 5fe46ebe64b3fe6c89840691a4172eb5852f0fba (diff) | |
download | cowboy-fda2d150db460dfe66f307d976be021b7fa47722.tar.gz cowboy-fda2d150db460dfe66f307d976be021b7fa47722.tar.bz2 cowboy-fda2d150db460dfe66f307d976be021b7fa47722.zip |
Fix error response when constraint validation fails
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_req.erl | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index e916976..40aa652 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -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 |