Source for file jException.lib.php
Documentation is available at jException.lib.php
- <?php
- /**
- * @package jelix
- * @subpackage core
- * @author Laurent Jouanneau
- * @contributor Sylvain de Vathaire, Julien Issler
- * @copyright 2005-2007 laurent Jouanneau, 2007 Sylvain de Vathaire
- * @copyright 2008 Julien Issler
- * @link http://www.jelix.org
- * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
- */
-
- /**
- * Exception handler for the framework.
- * Replace the default PHP Exception handler
- * @param Exception $exception the exception object
- */
- function jExceptionHandler($exception){
- global $gJConfig, $gJCoord;
-
- $msg = $exception->getMessage();
-
- $conf = $gJConfig->error_handling;
- $action = $conf['exception'];
-
- $doEchoByResponse=true;
-
- if($gJCoord->request == null){
-
- $msg = 'JELIX PANIC ! Error during initialization !! '.$msg;
- $doEchoByResponse = false;
-
- }elseif($gJCoord->response == null){
-
- $ret = $gJCoord->initDefaultResponseOfRequest();
- if(is_string($ret)){
- $doEchoByResponse = false;
- $msg = 'Double error ! 1)'. $ret.'; 2)'.$msg;
- }
- }
-
- $ip = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR']:'');
-
- // formatage du message de log
- $messageLog = strtr($conf['messageLogFormat'], array(
- '%date%' => date("Y-m-d H:i:s"),
- '%ip%' => $ip,
- '%code%' => $exception->getCode(),
- '%msg%' => $msg,
- '%file%' => $exception->getFile(),
- '%line%' => $exception->getLine(),
- '%typeerror%'=>'exception',
- '\t' =>"\t",
- '\n' => "\n"
- ));
-
- if(strpos($action , 'TRACE') !== false){
- $arr = $exception->getTrace();
- $messageLog.="\ttrace:";
- foreach($arr as $k=>$t){
- $messageLog.="\n\t$k\t".(isset($t['class'])?$t['class'].$t['type']:'').$t['function']."()\t";
- $messageLog.=(isset($t['file'])?$t['file']:'[php]').' : '.(isset($t['line'])?$t['line']:'');
- }
- $messageLog.="\n";
- }
-
- $echoAsked = false;
- // traitement du message
- if(strpos($action , 'ECHOQUIET') !== false){
- $echoAsked = true;
- if(!$doEchoByResponse){
- header("HTTP/1.1 500 Internal jelix error");
- header('Content-type: text/plain');
- echo 'JELIX PANIC ! Error during initialization !! ';
- }else
- $gJCoord->addErrorMsg('error', $exception->getCode(), $conf['quietMessage'], '', '');
- }elseif(strpos($action , 'ECHO') !== false){
- $echoAsked = true;
- if($doEchoByResponse)
- $gJCoord->addErrorMsg('error', $exception->getCode(), $msg, $exception->getFile(), $exception->getLine());
- else{
- header("HTTP/1.1 500 Internal jelix error");
- header('Content-type: text/plain');
- echo $messageLog;
- }
- }
- if(strpos($action , 'LOGFILE') !== false){
- error_log($messageLog,3, JELIX_APP_LOG_PATH.$conf['logFile']);
- }
- if(strpos($action , 'MAIL') !== false){
- error_log(wordwrap($messageLog,70),1, $conf['email'], str_replace(array('\\r','\\n'),array("\r","\n"),$conf['emailHeaders']));
- }
- if(strpos($action , 'SYSLOG') !== false){
- error_log($messageLog,0);
- }
-
- if($doEchoByResponse) {
- if ($gJCoord->response)
- $gJCoord->response->outputErrors();
- else if($echoAsked) {
- header("HTTP/1.1 500 Internal jelix error");
- header('Content-type: text/plain');
- foreach($gJCoord->errorMessages as $msg)
- echo $msg."\n";
- }
- }
-
- jSession::end();
- exit;
- }
-
-
- /**
- * Jelix Exception
- *
- * It handles locale messages. Message property contains the locale key,
- * and a new property contains the localized message.
- * @package jelix
- * @subpackage core
- */
- class jException extends Exception {
-
- /**
- * the locale key
- * @var string
- */
- protected $localeKey = '';
-
- /**
- * parameters for the locale key
- */
- protected $localeParams = array();
-
- /**
- * @param string $localekey a locale key
- * @param array $localeParams parameters for the message (for sprintf)
- * @param integer $code error code (can be provided by the localized message)
- * @param string $lang
- * @param string $charset
- */
- public function __construct($localekey, $localeParams = array(), $code = 1, $lang = null, $charset = null) {
-
- $this->localeKey = $localekey;
- $this->localeParams = $localeParams;
-
- try{
- $message = jLocale::get($localekey, $localeParams, $lang, $charset);
- }catch(Exception $e){
- $message = $e->getMessage();
- }
- if(preg_match('/^\s*\((\d+)\)(.+)$/m',$message,$m)){
- $code = $m[1];
- $message = $m[2];
- }
- parent::__construct($message, $code);
- }
-
- /**
- * magic function for echo
- * @return string localized message
- */
- /*public function __toString() {
- return $this->localizedMessage;
- }*/
-
- /**
- * getter for the locale parameters
- * @return string
- */
- public function getLocaleParameters(){
- return $this->localeParams;
- }
-
- /**
- * getter for the locale key
- * @return string
- */
- public function getLocaleKey(){
- return $this->localeKey;
- }
-
- }
-
- ?>
Documentation generated on Wed, 07 Sep 2011 13:47:18 +0200 by phpDocumentor 1.4.3