Commit 537b414a authored by Siebers, Michael's avatar Siebers, Michael
Browse files

Updated mapping from exceptions to http_reply exceptions

parent cb4a997b
......@@ -85,24 +85,27 @@ do_handle_explain_(AbsPath, Limit, Result, More) :-
% @see handle_explain/1 for the resulting handler errors
exception_to_handler_exception(not_irrelevant(AbsPath),
bad_request(not_irrelevant(AbsPath))).
exception_to_handler_exception(
error(existence_error(key, Key, _Parameters),_),
bad_request(missing_key(Key))).
exception_to_handler_exception(
error(type_error(dict, Dict), Context),
server_error(type_error(dict, Dict), Context)) :- is_dict(Dict).
exception_to_handler_exception(
error(type_error(Type, KeyName),_),
bad_request(wrong_type(KeyName, Type))).
server_error(type_error(dict, Dict), Context)).
exception_to_handler_exception(
error(unknown_key(KeyName, _Parameters),_),
bad_request(unknown_key(KeyName))).
error(existence_error(key, Key, _Parameters),_),
bad_request(missing_key(Key))).
exception_to_handler_exception(
error(instantiation_error,Context),
server_error(instantiation_error,Context)).
exception_to_handler_exception(
error(key_instantiation_error(KeyName, Type, Value),Context),
server_error(key_instantiation_error(KeyName, Type, Value),Context)).
exception_to_handler_exception(
error(uninstantiation_error(Argument),Context),
server_error(uninstantiation_error(Argument),Context)).
exception_to_handler_exception(
error(key_type_error(KeyName, Type, _Value),_),
bad_request(wrong_type(KeyName, Type))).
exception_to_handler_exception(
error(unknown_key(KeyName, _Parameters),_),
bad_request(unknown_key(KeyName))).
exception_to_handler_exception(
error(implementation_error(Cause),Context),
server_error(implementation_error(Cause),Context)).
......
......@@ -94,19 +94,16 @@ server_opts(Opts) :-
%
% Handles HTTP request querrying the irrelevance of a single absolute path.
%
% @throws bad_request(not_irrelevant(AbsPath)) If the absolut path provided in
% the request's payload denotes a
% file which is not irrelevant.
% Then, by definition, no
% explanation may be created.
% @throws bad_request(missing_key(Key)) If a required parameter was missing
% in the JSON payload.
% @throws bad_request(wrong_type(abs_path, atom))) If abs_path's value
% is not an atom.
% @throws bad_request(unknown_key(KeyName)) The unknown key KeyName was supplied
% in the request's payload.
% @throw server_error(E,Context) if an exception occured which is not the user's
% error, but our own.
% @throws http_reply(bad_request(Reason)) if he handler encounters an error which
% is caused by the client side. The following Reason's may be given:
% - missing_key(Key): If a required parameter was missing in the
% JSON payload.
% - wrong_type(abs_path, atom): The abs_path value must be given as
% JSON string.
% - unknown_key(KeyName): The unknown key KeyName was supplied in
% the request's payload.
% @throw http_reply(server_error(E,Context)) if an exception occured which is
% not the user's error, but our own.
handle_irrelevant(file,Req) :-
http_read_json(Req, Parameters, [json_object(dict), value_string_as(atom)]),
catch(
......@@ -117,14 +114,14 @@ handle_irrelevant(file,Req) :-
),
error(E,Context),
( E=existence_error(key,Key,_)
-> throw(bad_request(missing_key(Key)))
-> throw(http_reply(bad_request(missing_key(Key))))
; ( E=unknown_key(Key,_)
-> throw(bad_request(unknown_key(Key)))
-> throw(http_reply(bad_request(unknown_key(Key))))
; ( E=type_error(atom, Value)
-> ( string(Value) % should not happen as we try
% to parse atoms
-> throw(server_error(E,Context))
; throw(bad_request(wrong_type(abs_path, atom, Value)))
-> throw(http_reply(server_error(E,Context)))
; throw(http_reply(bad_request(wrong_type(abs_path, atom, Value))))
)
; ( once((
E=instantiation_error; % should not happen as there
......@@ -133,8 +130,8 @@ handle_irrelevant(file,Req) :-
% try to parse a dict
E=implementation_error(_) % should never happen
))
-> throw(server_error(E,Context))
; throw(server_error(unexpected_error(E),Context))
-> throw(http_reply(server_error(E,Context)))
; throw(http_reply(server_error(unexpected_error(E),Context)))
)
)
)
......@@ -145,32 +142,32 @@ handle_irrelevant(file,Req) :-
%
% Handles HTTP request to explain the irrelevance of a single absolute path.
%
% @throws bad_request(not_irrelevant(AbsPath)) If the absolut path provided in
% the request's payload denotes a
% file which is not irrelevant.
% Then, by definition, no
% explanation may be created.
% @throws bad_request(missing_key(Key)) If a required parameter was missing
% in the JSON payload.
% @throws bad_request(wrong_type(KeyName, Type))) If the value associated with
% the key KeyName is not of the
% correct type.
% @throws bad_request(unknown_key(KeyName)) The unknown key KeyName was supplied
% in the request's payload.
% @throw server_error(E,Context) if an exception occured which is not the user's
% error, but our own.
% @throws http_reply(bad_request(Reason)) if he handler encounters an error which
% is caused by the client side. The following Reason's may be given:
% - not_irrelevant(AbsPath): If the absolute path provided in the
% request's payload denotes a file which
% is not irrelevant. Then, by definition,
% no explanation may be created.
% - missing_key(Key): If a required parameter was missing in the
% JSON payload.
% - wrong_type(Key, Type): The value under key Key is of the wrong
% type, expected Type
% - unknown_key(KeyName): The unknown key KeyName was supplied in
% the request's payload.
% @throw http_reply(server_error(E,Context)) if an exception occured which is
% not the user's error, but our own.
handle_explain(Req) :-
http_read_json(Req, Parameters, [json_object(dict), value_string_as(atom)]),
catch(do_handle_explain(Parameters, Explanations, More),
E,
(
exception_to_handler_exception(E, HandlerException),
throw(HandlerException)
throw(http_reply(HandlerException))
)
),
catch(maplist(explanation_to_json, Explanations, ExplanationsJSON),
error(Err,Context),
throw(server_error(Err,Context))),
throw(http_reply(server_error(Err,Context)))),
reply_json_dict(reply{explanations: ExplanationsJSON,
further_explanations: @(More)},
[true(@(true)), false(@(false))] % is only default for
......
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