Commit 993f510e authored by Siebers, Michael's avatar Siebers, Michael
Browse files

Intercept goal_failed (within web_api module)

parent 506fc5dd
......@@ -39,9 +39,10 @@ prolog:message(missing_key(Key)) -->
* 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.
* 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)), [], []).
......@@ -136,4 +137,34 @@ item_not_relevant(Item) -->
* Server Errors *
******************/
/* error(goal_failed(web_api:Goal),C)
*
* Thrown by http_dispatch:call_action/2 if one of our handlers fails.
* Rethrown as error(api_error(goal_failed(PredIndicator)),C).
*
* N.B.: The SWI-Prolog documentation states "The hook is not allowed to modify
* ExceptionOut in such a way that it no longer unifies with the catching
* frame." The Exception is caught in httpd_wrapper:handler_with_output_to/5,
* where the catcher is unbound. Thus, we do not violate this constraint.
*/
user:prolog_exception_hook(error(goal_failed(web_api:Goal),Context),
error(api_error(Hash, goal_failed),Context), Frame, _) :-
Goal =.. [PredName | Args],
last(Args, Request),
error_hash(Request, Hash),
get_prolog_backtrace(20, Backtrace, [frame(Frame), clause_references(false)]),
http_log('incident(~a, ~q, ~q).~n',[Hash, goal_failed, [goal(Goal), prolog_stack(Backtrace), original_context(Context)]]).
%! error_hash(+Request, --Hash) is det
error_hash(Request, Hash) :-
get_time(TimeStamp),
random_between(0,1000000,RandInt),
variant_sha1( error_hash(TimeStamp, RandInt, Request), FullHash),
atom_chars(FullHash,HashChars),
length(HashStartChars, 16),
append(HashStartChars,_,HashChars),
format(atom(Hash), '~s~s~s~s-~s~s~s~s-~s~s~s~s-~s~s~s~s', HashStartChars).
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