aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_router.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-06-28 17:38:17 +0200
committerLoïc Hoguin <[email protected]>2017-06-28 17:38:17 +0200
commitc22173037134becbac882533f80339f4127a8ad2 (patch)
tree666fc6e31c9e02766eb33ecadc5068dd54e18aa4 /src/cowboy_router.erl
parent3eb7693e4fd7e1172b55b30ab25c5656040d1fd2 (diff)
downloadcowboy-c22173037134becbac882533f80339f4127a8ad2.tar.gz
cowboy-c22173037134becbac882533f80339f4127a8ad2.tar.bz2
cowboy-c22173037134becbac882533f80339f4127a8ad2.zip
Improve the interface for constraints
There are two important changes in this commit. Constraints are now producing an error tuple. This error tuple in turn can be provided to a function for formatting a human readable error message. Both the error tuple and the formatting code are controlled by and part of the constraint function. Constraints now also implement the reverse operation. When constraint functions only validate, the reverse operation will be the same as the forward operation. When they also do some conversion then the reverse operation will reverse it. Since constraints are now performing 3 different operations (forward, reverse and format_error), they now take the form of a function accepting two separate arguments. The operation is the first argument. In addition, the return value was changed to take the form of {ok, Value} | {error, Reason}. The value must be returned as-is if it was not modified.
Diffstat (limited to 'src/cowboy_router.erl')
-rw-r--r--src/cowboy_router.erl12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index 7e70025..53ead4a 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -277,14 +277,12 @@ check_constraints([Field|Tail], Bindings) when is_atom(Field) ->
check_constraints([Field|Tail], Bindings) ->
Name = element(1, Field),
case Bindings of
- #{Name := Value} ->
+ #{Name := Value0} ->
Constraints = element(2, Field),
- case cowboy_constraints:validate(Value, Constraints) of
- true ->
- check_constraints(Tail, Bindings);
- {true, Value2} ->
- check_constraints(Tail, Bindings#{Name => Value2});
- false ->
+ case cowboy_constraints:validate(Value0, Constraints) of
+ {ok, Value} ->
+ check_constraints(Tail, Bindings#{Name => Value});
+ {error, _} ->
nomatch
end;
_ ->