Source for file jBasicErrorHandler.class.php

Documentation is available at jBasicErrorHandler.class.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage core
  5. * @author     Laurent Jouanneau
  6. * @copyright  2012 Laurent Jouanneau
  7. * @link        http://jelix.org
  8. * @licence    http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  9. */
  10.  
  11.  
  12. /**
  13. * Error handlers for the framework.
  14. * Replace the default PHP error handler
  15. * @param   integer     $errno      error code
  16. * @param   string      $errmsg     error message
  17. * @param   string      $filename   filename where the error appears
  18. * @param   integer     $linenum    line number where the error appears
  19. * @param   array       $errcontext 
  20. */
  21.  
  22. class jBasicErrorHandler {
  23.     
  24.     static $errorCode = array(
  25.         E_ERROR         => 'error',
  26.         E_RECOVERABLE_ERROR => 'error',
  27.         E_WARNING       => 'warning',
  28.         E_NOTICE        => 'notice',
  29.         E_DEPRECATED    => 'deprecated',
  30.         E_USER_ERROR    => 'error',
  31.         E_USER_WARNING  => 'warning',
  32.         E_USER_NOTICE   => 'notice',
  33.         E_USER_DEPRECATED => 'deprecated',
  34.         E_STRICT        => 'strict'
  35.     );
  36.  
  37.     static function register() {
  38.         set_error_handler(array('jBasicErrorHandler', 'errorHandler'));
  39.         set_exception_handler(array('jBasicErrorHandler', 'exceptionHandler'));
  40.     }
  41.  
  42.     /**
  43.      * Error handler showing a simple error page
  44.      * Replace the default PHP error handler.
  45.      * @param   integer     $errno      error code
  46.      * @param   string      $errmsg     error message
  47.      * @param   string      $filename   filename where the error appears
  48.      * @param   integer     $linenum    line number where the error appears
  49.      * @param   array       $errcontext 
  50.      */
  51.     static function errorHandler($errno, $errmsg, $filename, $linenum, $errcontext) {
  52.  
  53.         if (error_reporting() == 0)
  54.             return;
  55.  
  56.         if (preg_match('/^\s*\((\d+)\)(.+)$/', $errmsg, $m)) {
  57.             $code = $m[1];
  58.             $errmsg = $m[2];
  59.         }
  60.         else {
  61.             $code = 1;
  62.         }
  63.  
  64.         if (!isset (self::$errorCode[$errno])){
  65.             $errno = E_ERROR;
  66.         }
  67.         $codestr = self::$errorCode[$errno];
  68.  
  69.         $trace = debug_backtrace();
  70.         array_shift($trace);
  71.         self::handleError($codestr, $errno, $errmsg, $filename, $linenum, $trace);        
  72.     }
  73.  
  74.     /**
  75.      * Exception handler showing a simple error page
  76.      * Replace the default PHP Exception handler
  77.      * @param   Exception   $e  the exception object
  78.      */
  79.     static function exceptionHandler($e) {
  80.         self::handleError('error', $e->getCode(), $e->getMessage(), $e->getFile(),
  81.                           $e->getLine(), $e->getTrace());
  82.     }
  83.  
  84.     static public $initErrorMessages = array();
  85.     
  86.     static function handleError ($type, $code, $message, $file, $line, $trace) {
  87.  
  88.         $errorLog = new jLogErrorMessage($type, $code, $message, $file, $line, $trace);
  89.  
  90.         // for non fatal error appeared during init, let's just store it for loggers later
  91.         if ($type != 'error') {
  92.             self::$initErrorMessages[] = $errorLog;
  93.             return;
  94.         }
  95.         else if (jServer::isCLI()) {
  96.             // fatal error appeared during init, in a CLI context
  97.  
  98.             while (ob_get_level() && @ob_end_clean());
  99.  
  100.             // log into file and output message in the console
  101.             echo 'Error during initialization: \n';
  102.             foreach(self::$initErrorMessages as $err) {
  103.                 @error_log($err->getFormatedMessage()."\n", 3, jApp::logPath('errors.log'));
  104.                 echo '* '.$err->getMessage().' ('.$err->getFile().' '.$err->getLine().")\n";
  105.             }
  106.  
  107.             @error_log($errorLog->getFormatedMessage()."\n", 3, jApp::logPath('errors.log'));
  108.             echo '* '.$message.' ('.$file.' '.$line.")\n";
  109.         }
  110.         else {
  111.             // fatal error appeared during init, let's display an HTML page
  112.             // since we don't know the request, we cannot return a response
  113.             // corresponding to the expected protocol
  114.  
  115.             while (ob_get_level() && @ob_end_clean());
  116.  
  117.             // log into file
  118.             foreach(self::$initErrorMessages as $err) {
  119.                 @error_log($err->getFormatedMessage()."\n", 3, jApp::logPath('errors.log'));
  120.             }
  121.             @error_log($errorLog->getFormatedMessage()."\n", 3, jApp::logPath('errors.log'));
  122.  
  123.             $msg = $errorLog->getMessage();
  124.             if (strpos($msg, '--') !== false) {
  125.                 list($msg, $bin) = explode('--', $msg, 2); // remove confidential data
  126.             }
  127.  
  128.             // if accept text/html
  129.             if (isset($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'],'text/html')) {
  130.                 if (file_exists(jApp::appPath('responses/error.en_US.php')))
  131.                     $file = jApp::appPath('responses/error.en_US.php');
  132.                 else
  133.                     $file = JELIX_LIB_CORE_PATH.'response/error.en_US.php';
  134.                 $HEADBOTTOM = '';
  135.                 $BODYTOP = '';
  136.                 $BODYBOTTOM = htmlspecialchars($msg);
  137.                 $BASEPATH = '/';
  138.                 if (jApp::config() && isset(jApp::config()->urlengine['basePath']))
  139.                     $BASEPATH = jApp::config()->urlengine['basePath'];
  140.                 header("HTTP/1.1 500 Internal jelix error");
  141.                 header('Content-type: text/html');
  142.                 include($file);
  143.             }
  144.             else {
  145.                 // output text response
  146.                 header("HTTP/1.1 500 Internal jelix error");
  147.                 header('Content-type: text/plain');
  148.                 echo 'Error during initialization. '.$msg;
  149.             }
  150.         }
  151.         exit(1);
  152.     }
  153. }

Documentation generated on Mon, 26 Oct 2015 21:51:49 +0100 by phpDocumentor 1.4.3