Commit 506fc5dd authored by Siebers, Michael's avatar Siebers, Michael
Browse files

Added error handling for missing_key/1.

Expanded comments to indicate which exception is thrown when.
parent fe113fb8
......@@ -8,8 +8,10 @@
* Request JSON errors *
************************/
/*
* unknown_key(Key) => bad request
/* unknown_key(Key) => bad request
*
* Client supplied a key-value pair for key Key, but the API
* endpoint does not know how to handle this key.
*/
http:map_exception_to_http_status_hook(unknown_key(Key),
bad_request(unknown_key(Key)), [], []).
......@@ -18,8 +20,28 @@ prolog:message(unknown_key(Key)) -->
["Unknown key in request: ", Key].
/*
* wrong_type(Key, ExpectedType, Value) => bad request
/* missing_key(Key) => bad request
*
* The client did not supply a key-value pair for key Key. However,
* the API endpoint requires the key to fullfil the request.
*/
http:map_exception_to_http_status_hook(missing_key(Key),
bad_request(missing_key(Key)), [], []).
prolog:message(missing_key(Key)) -->
["Required key missing in request: ", Key].
/* wrong_type(Key, ExpectedType, Value) => bad request
*
* The client supplied a key-value pair for key Key, but the API
* endpoint does not know how to handle the associated value as
* it expected the value to be of another type.
*
* N.B.: We throw this as Bad Request. However, we cannot check the
* type of the value prior to parsing. Thus, we are comparing prolog
* term with expected prolog types. So in principle this exception
* might also be thrown due to a parsing mistake, i.e. a server error.
*/
http:map_exception_to_http_status_hook(wrong_type(Key, ExpectedType, Value),
bad_request(wrong_type(Key, ExpectedType, Value)), [], []).
......@@ -58,7 +80,6 @@ json_type_name(Count, one_of(L), Str) :-
json_type_name_or(Count, L,0,_,Types),
format(string(Str), 'either ~s', [Types]).
json_type_name_or(Count, [Last],Before,0,LastStr) :-
!,
json_type_name(Count, Last, LastName),
......@@ -85,8 +106,11 @@ json_type_name_or(Count, [E|Es],Before,After,Str) :-
* Semantic Errors *
********************/
/*
* not_irrelevant(AbsPath) => bad request
/* not_irrelevant(AbsPath) => bad request
*
* The API endpoints expected the item at path AbsPath to be
* irrelevant. However, the item is not. Thus, the behavior of
* the endpoint is undefined.
*/
http:map_exception_to_http_status_hook(not_irrelevant(AbsPath),
bad_request(not_irrelevant(AbsPath)), [], []).
......
......@@ -90,7 +90,7 @@ server_opts(Opts) :-
*/
:- initialization http_handler(root(test), handle_test, []).
handle_test(_Request) :-
throw(wrong_type(abs_path,list(atom),1)).
throw(missing_key(a_key)).
/* Closures for handlers */
%! handle_irrelevant_file(++Request) is det
......@@ -117,7 +117,7 @@ handle_irrelevant(file,Req) :-
),
error(E,Context),
( E=existence_error(key,Key,_)
-> throw(http_reply(bad_request(missing_key(Key))))
-> throw(missing_key(Key))
; ( E=unknown_key(Key,_)
-> throw(unknown_key(Key))
; ( E=type_error(atom, Value)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment