Commit fe113fb8 authored by Siebers, Michael's avatar Siebers, Michael
Browse files

Provide uniform error handling for irrelevant_file/1, unknown_key/1, and wrong_type/3.

parent 9b883e20
:- multifile http:map_exception_to_http_status_hook/4,
prolog:error_message//1.
:- discontiguous http:map_exception_to_http_status_hook/4,
prolog:error_message//1.
/************************
* Request JSON errors *
************************/
/*
* unknown_key(Key) => bad request
*/
http:map_exception_to_http_status_hook(unknown_key(Key),
bad_request(unknown_key(Key)), [], []).
prolog:message(unknown_key(Key)) -->
["Unknown key in request: ", Key].
/*
* wrong_type(Key, ExpectedType, Value) => bad request
*/
http:map_exception_to_http_status_hook(wrong_type(Key, ExpectedType, Value),
bad_request(wrong_type(Key, ExpectedType, Value)), [], []).
prolog:message(wrong_type(Key, ExpectedType, Value)) -->
{ http_json:atom_json_term(ValueStr,Value,[as(string)]),
once(json_type_name(ExpectedType, JSONTypeStr)) },
["Value for key ", Key, " has wrong type.", nl,
"Expected ", JSONTypeStr, ", received: ", ValueStr].
json_type_name(Type,String) :- json_type_name(singular, Type, String).
json_type_name(singular, TextType, "a string") :-
member(TextType, [atom, codes, string, chars, text]).
json_type_name(plural,TextType, "strings") :-
member(TextType, [atom, codes, string, chars, text]).
json_type_name(singular, integer, "an integer").
json_type_name(plural, integer, "integers").
json_type_name(singular, boolean, "a boolean").
json_type_name(plural, boolean, "booleans").
json_type_name(singular, float, "a float").
json_type_name(plural, float, "floats").
json_type_name(singular, nonneg, "a nonnegative integer").
json_type_name(plural, nonneg, "nonnegative integers").
json_type_name(singular, positive_integer, "a positive integer").
json_type_name(plural, positive_integer, "positive integers").
json_type_name(singular, list, "a list").
json_type_name(plural, list, "lists").
json_type_name(singular, list(SubType), ListStr) :-
json_type_name(plural, SubType, SubTypeStr),
format(string(ListStr), 'a list of ~s', [SubTypeStr]).
json_type_name(plural, list(SubType), ListStr) :-
json_type_name(plural, SubType, SubTypeStr),
format(string(ListStr), 'lists of ~s', [SubTypeStr]).
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),
( Before==0
-> LastStr = LastName
; ( Before==1
-> format(string(LastStr), ' or ~s', [LastName])
; format(string(LastStr), ', or ~s', [LastName])
)
).
json_type_name_or(Count, [E|Es],Before,After,Str) :-
succ(Before, BeforeNext),
json_type_name_or(Count, Es,BeforeNext, AfterNext,TailStr),
succ(AfterNext,After),
json_type_name(Count, E, EName),
( Before==0
-> format(string(Str), '~s~s', [EName, TailStr])
; format(string(Str), ', ~s~s', [EName, TailStr])
).
/********************
* Semantic Errors *
********************/
/*
* not_irrelevant(AbsPath) => bad request
*/
http:map_exception_to_http_status_hook(not_irrelevant(AbsPath),
bad_request(not_irrelevant(AbsPath)), [], []).
prolog:message(not_irrelevant(AbsPath)) -->
{theory_bg:item(AbsPath, Item)}
-> item_not_relevant(Item)
; no_item(AbsPath).
no_item(AbsPath) -->
{format(string(AbsPathStr), '~w', [AbsPath])},
["Unknown path: ", AbsPathStr].
item_not_relevant(Item) -->
( { is_dict(Item, file) }
-> ["File"]
; ["Directory"]
),
[" not irrelevant :", Item.abs_path].
/******************
* Server Errors *
******************/
:- module(web_api, [server_main/0]).
:- include(include/common/error_handling).
:- include(include/common/api_response).
:- include('include/irrelevance.pl').
:- include('include/explanations.pl').
......@@ -84,6 +85,13 @@ server_opts(Opts) :-
:- http_handler(root(remove), handle_remove, []).
:- http_handler(root(query), handle_query, []).
/*
* debug only
*/
:- initialization http_handler(root(test), handle_test, []).
handle_test(_Request) :-
throw(wrong_type(abs_path,list(atom),1)).
/* Closures for handlers */
%! handle_irrelevant_file(++Request) is det
%
......@@ -111,7 +119,7 @@ handle_irrelevant(file,Req) :-
( E=existence_error(key,Key,_)
-> throw(http_reply(bad_request(missing_key(Key))))
; ( E=unknown_key(Key,_)
-> throw(http_reply(bad_request(unknown_key(Key))))
-> throw(unknown_key(Key))
; ( E=type_error(atom, Value)
-> ( string(Value) % should not happen as we try
% to parse atoms
......
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