Changeset 838 for trunk/Modules/Error


Ignore:
Timestamp:
Jan 9, 2016, 11:45:01 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Some libraries moved to Common package located at Packages directory.
  • Modified: Application class System renamed to Core. System class is not just basic parent class for application.
  • Fixed: alert file now use same application class as other files.
  • Modified: Error application module.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Error/Error.php

    r816 r838  
    33class ModuleError extends AppModule
    44{
    5   var $Encoding;
    6   var $ShowError;
    7   var $UserErrors;
    85  var $OnError;
     6  var $ErrorHandler;
    97
    10   function __construct($System)
     8  function __construct(System $System)
    119  {
    1210    parent::__construct($System);
     
    1816    $this->Dependencies = array();
    1917
    20     $this->Encoding = 'utf-8';
    21     $this->ShowError = false;
    22     $this->UserErrors = E_ALL;  //E_ERROR | E_WARNING | E_PARSE;
    2318    $this->OnError = array();
     19    $this->ErrorHandler = new ErrorHandler();
     20    $this->ErrorHandler->OnError[] = array($this, 'DoError');
    2421  }
    2522
    26   function Install()
     23  function DoStart()
    2724  {
    28     parent::Install();
     25    $this->ErrorHandler->Start();
    2926  }
    3027
    31   function UnInstall()
     28  function DoStop()
    3229  {
    33     parent::UnInstall();
     30    $this->ErrorHandler->Stop();
    3431  }
    3532
    36   function Start()
     33  function DoError($Error)
    3734  {
    38     parent::Start();
    39     set_error_handler(array($this, 'ErrorHandler'));
    40     set_exception_handler(array($this, 'ExceptionHandler'));
    41   }
    42 
    43   function Stop()
    44   {
    45     restore_error_handler();
    46     restore_exception_handler();
    47     parent::Stop();
    48   }
    49 
    50   function ErrorHandler($Number, $Message, $FileName, $LineNumber, $Variables)
    51   {
    52     $ErrorType = array
    53     (
    54       1 => 'Error',
    55       2 => 'Warning',
    56       4 => 'Parsing Error',
    57       8 => 'Notice',
    58       16 => 'Core Error',
    59       32 => 'Core Warning',
    60       64 => 'Compile Error',
    61       128 => 'Compile Warning',
    62       256 => 'User Error',
    63       512 => 'User Warning',
    64       1024 => 'User Notice'
    65     );
    66 
    67     if(($this->UserErrors & $Number))
    68     {
    69       $Backtrace = debug_backtrace();
    70       $Backtrace[0]['function'] = $Message;
    71       $Backtrace[0]['args'] = '';
    72       $Backtrace[0]['file'] = $FileName;
    73       $Backtrace[0]['line'] = $LineNumber;
    74       $this->Report($Backtrace);
    75       //if((E_ERROR | E_PARSE) & $Number)
    76       die();
    77     }
    78   }
    79 
    80   function ExceptionHandler(Exception $Exception)
    81   {
    82     $Backtrace = $Exception->getTrace();
    83     array_unshift($Backtrace, array(
    84       'function' => $Exception->getMessage(),
    85       'file' => $Exception->getFile(),
    86       'line' => $Exception->getLine(),
    87     ));
    88     $this->Report($Backtrace);
    89     die();
    90   }
    91 
    92   function Report($Backtrace)
    93   {
    94     $Date = date('Y-m-d H:i:s');
    95     $Error = '# '.$Date."\n";
    96     foreach($Backtrace as $Item)
    97     {
    98       if(!array_key_exists('line', $Item)) $Item['line'] = '';
    99       if(!array_key_exists('file', $Item)) $Item['file'] = '';
    100 
    101       $Error .= ' '.$Item['file'].'('.$Item['line'].")\t".$Item['function'];
    102       $Arguments = '';
    103       if(array_key_exists('args', $Item) and is_array($Item['args']))
    104         foreach($Item['args'] as $Item)
    105         {
    106           if(is_object($Item)) ;
    107           else if(is_array($Item)) $Arguments .= "'".serialize($Item)."',";
    108           else $Arguments .= "'".$Item."',";
    109         }
    110         if(strlen($Arguments) > 0) $Error .= '('.substr($Arguments, 0, -1).')';
    111         $Error .= "\n";
    112     }
    113     $Error .= "\n";
    114 
    115     // Show error message
    116     $Output = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>'."\n".
    117       '<meta http-equiv="Content-Language" content="cs">'."\n".
    118       '<meta http-equiv="Content-Type" content="text/html; charset='.$this->Encoding.'"></head><body>'."\n".
    119       T('An internal error occurred! <br /> Administrator has been made ​​aware of it and the error soon will be removed.').'<br/><br/>';
    120     if($this->ShowError == true)
    121       $Output .= '<pre>'.$Error.'</pre><br/>';
    122     $Output .= '</body></html>';
    123     echo($Output);
    12435    foreach($this->OnError as $OnError)
    125       $OnError[0]->$OnError[1]($Error);
     36      call_user_func($OnError, $Error);
    12637  }
    12738}
Note: See TracChangeset for help on using the changeset viewer.