Fail/0 caused Internal server error

I’m using: SWI-Prolog version 7.6.4
Why fail/0 causes Internal server error?
Thanks.

A bit of context please? Preferably something we can run, but at least some code and a description of the context it is used in.

example:-
testinput.pl

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_parameters)).
:- use_module(testoutput).
:- http_handler(root(test), web_form, []).

server(Port) :-
        http_server(http_dispatch, [port(Port)]).
web_form(_Request) :-
	reply_html_page(
            [title('e-TEST')],
	    [
	     form([action='/landing', method='POST'], [
		p([], [
		  label([for=name],'Please select:     '),
		  input([name=test, type=radio, value="one"]),"TEST 1",
                  input([name=test, type=radio, value="two"]),"TEST 2"
		      ]),
		p([], input([name=submit, type=submit, value='Submit'], []))
	      ])]).

:- http_handler(root(landing), user_input, []).

user_input(Request):-
        http_parameters(Request,
                        [ test(Input, [])
                        ]),
       format('Content-type: text/html~n~n', []),
       format('<table border=1>', []),
       format('<tr>', []),
       format('<td>', []),write("Your Input"),nl,format('</td>', []),
       format('<td>', []),format('<b>', []),write(Input),nl,format('</b>', []),format('</td>', []),
       format('</tr>', []),
       format('</table>', []),
       test_output(Input).

testoutput.pl

%testoutput.pl
:- module(testoutput,
  [
    test_output/1
  ]).

test_output(X):-
    writeln(X),
    output.

output:-
    my_list(_, Output),
    writeln(Output),
    fail.

my_list(1, 'test 1').
my_list(2, 'test 2').
my_list(3, 'test 3').
my_list(4, 'test 4').
my_list(5, 'test 5').
my_list(6, 'test 6').
my_list(7, 'test 7').

So, yes. Failure of a declared HTTP handler is considered an internal server error. Some exceptions are mapped to specific HTTP codes, but most also end up as internal sever error. What else would you expect?

1 Like

any solution for this?

It’s not a problem. Don’t fail inside a handler if you don’t want an internal server error.

In this case, you could add a fact

output.

to the bottom of your output/0 predicate to stop it from failing at the end.

2 Likes