View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2006-2016, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(http_error,
   37          [
   38          ]).   39:- use_module(library(prolog_stack)).   40:- use_module(library(settings)).   41:- use_module(library(broadcast)).   42:- use_module(library(debug)).

Decorate uncaught HTTP exceptions with stack-trace

This module decorates uncaught exceptions of the user code with a full stack-trace and sends error reports to the Prolog console. The behaviour can be controlled by

nodebug(http(error))
After disabling the http(error) debug channal, errors are only sent to the client. See nodebug/1 and debug/1.
set_setting(http:client_backtrace, false)
Stop sending stack traces to the client. Note that sending the stack trace to the client simplifies debugging, it also provides clues to hackers on how to compromise your site. The more information you give them, the easier it is to break into your server! See set_setting/2 and set_setting_default/2. */
   61:- setting(http:client_backtrace, boolean, true,
   62           'Make backtrace visible to the client').   63
   64
   65                 /*******************************
   66                 *     LOG ERRORS TO STDERR     *
   67                 *******************************/
   68
   69:- debug(http(error)).   70
   71:- listen(http(Message),
   72          http_listen(Message)).   73
   74:- dynamic
   75    saved_request/2.   76
   77http_listen(_) :-
   78    \+ debugging(http(error)),
   79    !.
   80http_listen(request_start(Id, Request)) :-
   81    !,
   82    asserta(saved_request(Id, Request)).
   83http_listen(request_finished(Id, Code, Status, _CPU, _Bytes)) :-
   84    retract(saved_request(Id, Request)),
   85    !,
   86    Code >= 400,
   87    memberchk(path(Path), Request),
   88    memberchk(method(Method), Request),
   89    upcase_atom(Method, UMethod),
   90    reply_status(Status, Reply),
   91    debug(http(error),
   92          '~w ~w: [~w] ~w', [UMethod, Path, Code, Reply]).
   93
   94reply_status(Status, Reply) :-
   95    map_exception(Status, Reply),
   96    !.
   97reply_status(Status, Message) :-
   98    message_to_string(Status, Message).
   99
  100map_exception(http_reply(bytes(ContentType,Bytes),_), bytes(ContentType,L)) :-
  101    string_length(Bytes, L).        % also does lists
  102map_exception(http_reply(Reply), Reply).
  103map_exception(http_reply(Reply, _), Reply).
  104map_exception(error(existence_error(http_location, Location), _Stack),
  105              error(404, Location)).
  106
  107
  108                 /*******************************
  109                 *     DECORATE STACK TRACES    *
  110                 *******************************/
  111
  112:- dynamic prolog_stack:stack_guard/1.  113:- multifile prolog_stack:stack_guard/1.  114
  115prolog_stack:stack_guard(httpd_wrapper:wrapper/5).
  116prolog_stack:stack_guard(httpd_wrapper:handler_with_output_to/5)