Did you know ... Search Documentation:
http_exception.pl -- Map Prolog exceptions to HTTP errors
PublicShow source

This module maps exceptions from various parts of the HTTP libraries as well as exceptions from user handler predicates into meaningful HTTP error codes such as 4XX and 5XX codes. For example, existence errors on http locations are mapped to 404 while out-of-stack is mapped to 503.

This library provides two hooks:

  • map_exception_to_http_status_hook/4 can be used to map additional exceptions to any HTTP status code.
  • http:bad_request_error/2 can be extended to map exceptions into 400 bad request responses.
See also
- http_header.pl, http_wrapper.pl
Source map_exception_to_http_status(+Exception, -Reply, -HdrExtra, -Context)
Map certain exceptions to HTTP status codes. The http(not_modified) provides backward compatibility to http_reply(not_modified).
Source in_or_exclude_backtrace(+ErrorIn, -ErrorOut)
Remove the stacktrace from the exception, unless setting http:client_backtrace is true.
 http:bad_request_error(+Formal, -ContextTemplate) is semidet[multifile]
If an exception of the term error(Formal, context(Stack, Context)) is caught and subsumes_term(ContextTemplate, Context) is true, translate the exception into an HTTP 400 exception. If the exception contains a stack-trace, this is stripped from the response.

The idea behind this hook is that applications can raise 400 responses by

  • Throwing a specific (error) exception and adding a rule to this predicate to interpret this as 400.
  • Define rules for error_message//1 to formulate an appropriate message.