Http/http_error and http_log

Dear list,

I noticed some strange interaction between http_error and http_log. It is ilustrated by the code snippet
below, which is a copy of Annie’s hello_web.pl + 3 lines of code.

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_log)).         %%% This line has been added
% :- use_module(library(http/http_error)).     %%% This line has been added

% The predicate server(+Port) starts the server. It simply creates a
% number of Prolog threads and then returns to the toplevel, so you can
% (re-)load code, debug, etc.
server(Port) :-
        http_server(http_dispatch, [port(Port)]).

% Declare a handler, binding an HTTP path to a predicate.
% Here our path is / (the root) and the goal we'll query will be
% say_hi. The third argument is for options
:- http_handler(/, say_hi, []).

/* The implementation of /. The single argument provides the request
details, which we ignore for now. Our task is to write a CGI-Document:
a number of name: value -pair lines, followed by two newlines, followed
by the document content, The only obligatory header line is the
Content-type: <mime-type> header.
Printing can be done using any Prolog printing predicate, but the
format-family is the most useful. See format/2.   */

say_hi(_Request) :-
        A is 1/0,                                    %%% This line has been added
        format('Content-type: text/plain~n~n'),
        format('Hello World!~n').

Running the server with

?- server(8001).

and invoking http://localhost:8001/ shows the expected error message (Internal server error, and then something with zero_divisor). However, if I uncomment Line 4, the error is not shown in the browser. Instead, the program seems to try to write something into the httpd.log file, which doesn’t work either.

This is the content of my screen. I hope it makes any sense.

matthias@DESKTOP-A2T8IFC:~/mcclass$ swipl s4.pl
Warning: /home/matthias/mcclass/s4.pl:25:
Warning:    Singleton variables: [A]
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.4-34-g8c2cd4a78-DIRTY)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- server(8001).
% Started server at http://localhost:8001/
true.

?- % [Thread httpd@8001_5] GET /: [500] format/3: Type error: `text' expected, found `ansi(code,'~p',[_15034 is 1/0])' (a compound)
ERROR: [Thread httpd@8001_5] format/3: Type error: `text' expected, found `ansi(code,'~p',[_19754 is 1/0])' (a compound)
% [Thread httpd@8001_2] GET /: [500] format/3: Type error: `text' expected, found `ansi(code,'~p',[_1278 is 1/0])' (a compound)
ERROR: [Thread httpd@8001_2] format/3: Type error: `text' expected, found `ansi(code,'~p',[_1368 is 1/0])' (a compound)
% [Thread httpd@8001_4] GET /: [500] format/3: Type error: `text' expected, found `ansi(code,'~p',[_1278 is 1/0])' (a compound)
ERROR: [Thread httpd@8001_4] format/3: Type error: `text' expected, found `ansi(code,'~p',[_1368 is 1/0])' (a compound)
etc., a few more lines like this

My current workaround is to not use the module http_error. Informative errors are shown in the browser anyway (which I think disagrees with the documentation).

Best regards,

Matthias

1 Like

Thanks for reporting! This is a regression from using color in messages that were not dealt with in the translation to HTML. Pushed a fix.

2 Likes