Source for file jException.lib.php

Documentation is available at jException.lib.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage core
  5. * @author     Laurent Jouanneau
  6. * @contributor Sylvain de Vathaire, Julien Issler
  7. * @copyright  2005-2007 laurent Jouanneau, 2007 Sylvain de Vathaire
  8. * @copyright  2008 Julien Issler
  9. * @link        http://www.jelix.org
  10. * @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  11. */
  12.  
  13. /**
  14. * Exception handler for the framework.
  15. * Replace the default PHP Exception handler
  16. * @param   Exception   $exception  the exception object
  17. */
  18. function jExceptionHandler($exception){
  19.     global $gJConfig, $gJCoord;
  20.  
  21.     $msg = $exception->getMessage();
  22.  
  23.     $conf = $gJConfig->error_handling;
  24.     $action = $conf['exception'];
  25.  
  26.     $doEchoByResponse=true;
  27.  
  28.     if($gJCoord->request == null){
  29.  
  30.         $msg = 'JELIX PANIC ! Error during initialization !! '.$msg;
  31.         $doEchoByResponse = false;
  32.  
  33.     }elseif($gJCoord->response == null){
  34.  
  35.         $ret = $gJCoord->initDefaultResponseOfRequest();
  36.         if(is_string($ret)){
  37.             $doEchoByResponse = false;
  38.             $msg = 'Double error ! 1)'. $ret.'; 2)'.$msg;
  39.         }
  40.     }
  41.  
  42.     $ip = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR']:'');
  43.  
  44.     // formatage du message de log
  45.     $messageLog = strtr($conf['messageLogFormat'], array(
  46.         '%date%' => date("Y-m-d H:i:s"),
  47.         '%ip%'   => $ip,
  48.         '%code%' => $exception->getCode(),
  49.         '%msg%'  => $msg,
  50.         '%file%' => $exception->getFile(),
  51.         '%line%' => $exception->getLine(),
  52.         '%typeerror%'=>'exception',
  53.         '\t' =>"\t",
  54.         '\n' => "\n"
  55.     ));
  56.  
  57.     if(strpos($action , 'TRACE') !== false){
  58.         $arr = $exception->getTrace();
  59.         $messageLog.="\ttrace:";
  60.         foreach($arr as $k=>$t){
  61.             $messageLog.="\n\t$k\t".(isset($t['class'])?$t['class'].$t['type']:'').$t['function']."()\t";
  62.             $messageLog.=(isset($t['file'])?$t['file']:'[php]').' : '.(isset($t['line'])?$t['line']:'');
  63.         }
  64.         $messageLog.="\n";
  65.     }
  66.  
  67.     $echoAsked = false;
  68.     // traitement du message
  69.     if(strpos($action , 'ECHOQUIET') !== false){
  70.         $echoAsked = true;
  71.         if(!$doEchoByResponse){
  72.             header("HTTP/1.1 500 Internal jelix error");
  73.             header('Content-type: text/plain');
  74.             echo 'JELIX PANIC ! Error during initialization !! ';
  75.         }else
  76.             $gJCoord->addErrorMsg('error', $exception->getCode(), $conf['quietMessage'], '', '');
  77.     }elseif(strpos($action , 'ECHO') !== false){
  78.         $echoAsked = true;
  79.         if($doEchoByResponse)
  80.             $gJCoord->addErrorMsg('error', $exception->getCode(), $msg, $exception->getFile(), $exception->getLine());
  81.         else{
  82.             header("HTTP/1.1 500 Internal jelix error");
  83.             header('Content-type: text/plain');
  84.             echo $messageLog;
  85.         }
  86.     }
  87.     if(strpos($action , 'LOGFILE') !== false){
  88.         error_log($messageLog,3, JELIX_APP_LOG_PATH.$conf['logFile']);
  89.     }
  90.     if(strpos($action , 'MAIL') !== false){
  91.         error_log(wordwrap($messageLog,70),1, $conf['email'], str_replace(array('\\r','\\n'),array("\r","\n"),$conf['emailHeaders']));
  92.     }
  93.     if(strpos($action , 'SYSLOG') !== false){
  94.         error_log($messageLog,0);
  95.     }
  96.  
  97.     if($doEchoByResponse) {
  98.         if ($gJCoord->response)
  99.             $gJCoord->response->outputErrors();
  100.         else if($echoAsked) {
  101.             header("HTTP/1.1 500 Internal jelix error");
  102.             header('Content-type: text/plain');
  103.             foreach($gJCoord->errorMessages as $msg)
  104.                 echo $msg."\n";
  105.         }
  106.     }
  107.  
  108.     jSession::end();
  109.     exit;
  110. }
  111.  
  112.  
  113. /**
  114.  * Jelix Exception
  115.  *
  116.  * It handles locale messages. Message property contains the locale key,
  117.  * and a new property contains the localized message.
  118.  * @package  jelix
  119.  * @subpackage core
  120.  */
  121. class jException extends Exception {
  122.  
  123.     /**
  124.      * the locale key
  125.      * @var string 
  126.      */
  127.     protected $localeKey = '';
  128.  
  129.     /**
  130.      * parameters for the locale key
  131.      */
  132.     protected $localeParams = array();
  133.  
  134.     /**
  135.      * @param string $localekey a locale key
  136.      * @param array $localeParams parameters for the message (for sprintf)
  137.      * @param integer $code error code (can be provided by the localized message)
  138.      * @param string $lang 
  139.      * @param string $charset 
  140.      */
  141.     public function __construct($localekey, $localeParams = array(), $code = 1, $lang = null, $charset = null) {
  142.  
  143.         $this->localeKey = $localekey;
  144.         $this->localeParams = $localeParams;
  145.  
  146.         try{
  147.             $message = jLocale::get($localekey, $localeParams, $lang, $charset);
  148.         }catch(Exception $e){
  149.             $message = $e->getMessage();
  150.         }
  151.         if(preg_match('/^\s*\((\d+)\)(.+)$/m',$message,$m)){
  152.             $code = $m[1];
  153.             $message = $m[2];
  154.         }
  155.         parent::__construct($message, $code);
  156.     }
  157.  
  158.     /**
  159.      * magic function for echo
  160.      * @return string localized message
  161.      */
  162.     /*public function __toString() {
  163.         return $this->localizedMessage;
  164.     }*/
  165.  
  166.     /**
  167.      * getter for the locale parameters
  168.      * @return string 
  169.      */
  170.     public function getLocaleParameters(){
  171.         return $this->localeParams;
  172.     }
  173.  
  174.     /**
  175.      * getter for the locale key
  176.      * @return string 
  177.      */
  178.     public function getLocaleKey(){
  179.         return $this->localeKey;
  180.     }
  181.  
  182. }
  183.  
  184. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:18 +0200 by phpDocumentor 1.4.3