I am writing a http_server
application with http_session
s in Prolog. A request is handled with a dicontiguous
(multiple definitions) predicate app
, which must not fail (otherwise a 500 error appears to be given to the client). I need to early-quit the predicate somehow, so that the rest is not ran, but a custom message is shown and it ends without failure:
:- discontiguous app/1.
app(Request) :-
ensure_path(Request), !, % I want a `!` after I determined the path is valid, so no other `app` is ran
% grab session id as `Session`
http_session_id(Session),
% try to get session data, fail otherwise (I check something else in my code, but `http_session_data` should suffice for this example)
(http_session_data(my_data(Data), Session); show_error, ...), % some syntactic magic here...
% render something if session data has value
render_some_success_html(Data).
Here is a piece of pseudocode of a non-Prolog language (don’t scold me for comparing languages of different paradigms) that I am trying to do:
function app(request) {
if (!ensure_path(request)) ...; // try other app(request)
let session = http_session_id();
let data = http_session_data('my_data', session);
if (!data) {
show_error();
return true; // this is important
}
render_some_success_html(data);
}
I tried to use a !, fail
in place of ...
, but that does a failure (swipl says goal unexpectedly failed
), and I need a true
instead.