neil_pearce

Exception handling

May 16th, 2022
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. /*
  2.  * Configure error reporting
  3.  */
  4. if (is_webserver()) {
  5.     /* running under a web server */
  6.     ini_set('html_errors', '1');
  7.     ini_set('display_errors', defined('APPLICATION_DEVENV') ? '1' : '0');
  8. } else {
  9.     /* Command Line Interface (CLI) */
  10.     ini_set('html_errors', '0');
  11.     ini_set('display_errors', 'stderr');
  12. }
  13.  
  14. ini_set('log_errors', '0');
  15. ini_set('error_log', <path to directory> . '\error.log'));
  16.  
  17. /* define the default exception handler */
  18. set_exception_handler('exception_handler');
  19.  
  20. /* convert errors to exceptions (fatal errors are not handled by a user-defined error handler) */
  21. set_error_handler(function (int $err_type, string $err_message, string $err_file = '', int $err_line = 0) {
  22.     throw new ErrorException($err_message, 0, $err_type, $err_file, $err_line);
  23. });
  24.  
  25. /* handle fatal errors */
  26. register_shutdown_function(function () {
  27.     /* only unhandled errors (ie fatal errors) should have the last error set */
  28.     if (! is_null($err = error_get_last())) {
  29.         if (in_array($err['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR])) {
  30.             /* execution halted - record the error information on the error log */
  31.             error_log(sprintf('Fatal error: %s in %s: %d',
  32.                               $err['message'], $err['file'], $err['line']));
  33.         } else {
  34.             /* convert fatal errors to a CALL to the default exception handler */
  35.             $e = new ErrorException($err['message'], 0, $err['type'], $err['file'], $err['line']);
  36.             exception_handler($e);
  37.         }
  38.     }
  39. });
  40.  
  41.  
  42.  
  43. /**
  44.  * Default exception handler if an exception is not caught in a try/catch block.
  45.  *
  46.  * @param \Throwable $t Exception/Error
  47.  *
  48.  * @return void
  49.  */
  50. function exception_handler(\Throwable $t): void
  51. {
  52.     ... filter out missing web assets, renew session, determine http code, etc ...
  53.  
  54.     /* record the exception on the error log */
  55.     error_log(sprintf('%s: %s in %s: %d',
  56.                       get_class($t), $t->getMessage(), $t->getFile(), $t->getLine()));
  57.  
  58.     /*
  59.      * Display the exception
  60.      */
  61.     if (filter_var(ini_get('html_errors'), FILTER_VALIDATE_BOOL)) {
  62.         http_response_code($http_code);
  63.  
  64.         if (filter_var(ini_get('display_errors'), FILTER_VALIDATE_BOOL)) {
  65.             /* generate stack trace (development environment) */
  66.             exception_stacktrace($t);
  67.         } else {
  68.             /* generate user friendly exception page */
  69.             exception_infopage($t);
  70.         }
  71.     } else {
  72.         echo PHP_EOL, $t->getMessage(), PHP_EOL;
  73.     }
  74. }
  75.  
  76.  
  77.  
  78. /**
  79.  * Determine whether running under a web server.
  80.  *
  81.  * @return bool Indicates whether running under a web server
  82.  */
  83. function is_webserver(): bool
  84. {
  85.     return ! (defined('STDIN') || ! empty($_SERVER['argc']));
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment