Changes in error handling

As a followup to Automatic tests can succeed with syntax errors and similar topics raised in the past I’ve pushed a number of changes:

  • Add global and thread statistics/2 keys errors and warnings to count errors and warnings. This only counts errors and warnings printed using print_message/2 that are not intercepted using message_hook/3.

  • Add an option --on-error= and --on-warning= and related Prolog flags on_error and on_warning that can all have the values print (default), status (continue as normal, but exit with status 1) and halt (halt with status 1 on first error/warning).

  • halt/0 interprets the on-error=status (and for warnings) to either call halt(0) or halt(1). Directly calling halt/1 bypasses this.

  • Change -c file compilation to set the default on_error to status. This causes the system to load all code, check for errors and if there are no errors proceed with saving the state (and still report an error should some error message be printed).

  • Use a silent print_message/2 at the end of compiling a file if loading the file causes an error or warning. Note that this also traps errors in initialization/1,2.

  • Run all system tests with --on-error=status, so also syntax errors cause tests to fail.

By default everything remains compatible except for swipl -c file returns non-zero exit state on an error and does not produce a saved state. Using swipl --on-error=print -c file you get the old behavior.

I think this covers most scenarios. Please jump in if you see anything wrong with this.

4 Likes