Example #1
0
 public static function formatException(\Exception $e, $message = '', \Lampcms\I18n\Translator $Tr = null)
 {
     $message = !empty($message) ? $message : $e->getMessage();
     if ($e instanceof \Lampcms\DevException) {
         /**
          * @todo if Tr was passed here
          *       then we can translate string
          */
         $message = defined('LAMPCMS_DEBUG') && true === LAMPCMS_DEBUG ? $e->getMessage() : 'Error occurred. Administrator has been notified of the error. We will fix this as soon as possible';
         //$oTr->get('generic_error', 'exceptions');
     }
     /**
      * htmlspecialchars is for safety to prevent XSS injection in case
      * part of the error message comes from any type of user input
      * For example a string containing script injection (HTML tags) is passed in GET request
      * the error is then generated and the original string may before part of that
      * error message
      */
     if (!$e instanceof \Lampcms\UnactivatedException) {
         $message = \htmlspecialchars($message);
     }
     $aArgs = $e instanceof \Lampcms\Exception ? $e->getArgs() : null;
     $message = !empty($aArgs) ? \vsprintf($message, $aArgs) : $message;
     if ($Tr) {
         $message = $Tr[$message];
     }
     $sError = '';
     $sTrace = self::getExceptionTraceAsString($e) . "\n";
     $strFile = $e->getFile();
     $intLine = $e->getLine();
     $intCode = $e instanceof \ErrorException ? $e->getSeverity() : $e->getCode();
     $sLogMessage = 'Exception caught: ' . \get_class($e) . "\n" . $message . "\n" . 'error code: ' . $intCode . "\n" . 'file: ' . $strFile . "\n" . 'line: ' . $intLine . "\n" . 'stack: ' . $sTrace . "\n";
     d($sLogMessage . "\n" . '$_REQUEST: ' . \print_r($_REQUEST, true));
     if (!empty($_SESSION)) {
         d('$_SESSION: ' . print_r($_SESSION, 1));
     }
     $sError .= $message . "\n";
     if (defined('LAMPCMS_DEBUG') && true === LAMPCMS_DEBUG) {
         $sError .= "\nException Class: " . \get_class($e) . BR;
         $sError .= "\nError code: {$intCode}\n";
         $sError .= "\nFile: {$strFile}\n";
         $sError .= "\nLine: {$intLine}\n";
         if (!empty($sTrace)) {
             $sError .= "\nTrace: \n{$sTrace}\n";
         }
     }
     /**
      * If this exception has E_USER_WARNING error code
      * then it was thrown when parsing some ajax-based
      * request.
      *
      * We then need to only send json array with
      * only one key 'exception'
      */
     if (E_USER_NOTICE === $e->getCode() || Request::isAjax()) {
         /**
          * if this exception was thrown when uploading
          * file to iframe, then we need to add 'true' as
          * the last (2nd arg) to fnSendJson
          */
         $a = array('exception' => $sError, 'errHeader' => 'Error', 'type' => get_class($e));
         if ($e instanceof \Lampcms\FormException) {
             $a['fields'] = $e->getFormFields();
         }
         d('json array of exception: ' . print_r($a, 1));
         Responder::sendJSON($a);
     }
     return $sError;
 }
Example #2
0
 public static function formatException(\Exception $e, $sMessage = '', \Lampcms\I18n\Translator $Tr = null)
 {
     $sMessage = !empty($sMessage) ? $sMessage : $e->getMessage();
     //$sMessage = $e->getMessage();
     //$bHtml = ($e instanceof \Lampcms\Exception) ? $e->getHtmlFlag() : false;
     if ($e instanceof Lampcms\DevException) {
         $sMessage = defined('LAMPCMS_DEBUG') && true === LAMPCMS_DEBUG ? $e->getMessage() : 'Error occured';
         //$oTr->get('generic_error', 'exceptions');
     }
     // not sure why strip_tags was here
     //$sMessage = strip_tags($sMessage);
     $aArgs = $e instanceof \Lampcms\Exception ? $e->getArgs() : null;
     $sMessage = !empty($aArgs) ? vsprintf($sMessage, $aArgs) : $sMessage;
     if ($Tr) {
         $sMessage = $Tr[$sMessage];
     }
     $sError = '';
     $sTrace = nl2br(self::getExceptionTraceAsString($e)) . '<hr>';
     $strFile = $e->getFile();
     $intLine = $e->getLine();
     $intCode = $e instanceof \ErrorException ? $e->getSeverity() : $e->getCode();
     $sLogMessage = 'LampcmsError exception caught: ' . $sMessage . "\n" . 'error code: ' . $intCode . "\n" . 'file: ' . $strFile . "\n" . 'line: ' . $intLine . "\n" . 'stack: ' . $sTrace . "\n";
     d($sLogMessage . "\n" . '$_REQUEST: ' . print_r($_REQUEST, true));
     if (!empty($_SESSION)) {
         d('$_SESSION: ' . print_r($_SESSION, 1));
     }
     $sError .= $sMessage . "\n";
     if (defined('LAMPCMS_DEBUG') && true === LAMPCMS_DEBUG) {
         $sError .= 'error code: ' . $intCode . "\n";
         $sError .= 'file: ' . $strFile . "\n";
         $sError .= 'line: ' . $intLine . "\n";
         if (!empty($sTrace)) {
             $sError .= "\n<br><strong>Trace</strong>: \n<br>{$sTrace}\n<br>";
         }
     }
     d('cp');
     /**
      * If this exception has E_USER_WARNING error code
      * then it was thrown when parsing some ajax-based
      * request.
      *
      * We then need to only send json array with
      * only one key 'exception'
      */
     if (E_USER_NOTICE === $e->getCode() || Request::isAjax()) {
         /**
          * if this exception was thrown when uploading
          * file to iframe, then we need to add 'true' as
          * the last (2nd arg) to fnSendJson
          */
         $a = array('exception' => $sError, 'errHeader' => 'Error', 'type' => get_class($e));
         if ($e instanceof Lampcms\FormException) {
             $a['fields'] = $e->getFormFields();
         }
         d('json array of exception: ' . print_r($a, 1));
         Responder::sendJSON($a);
     }
     d('$sError: ' . $sError);
     return $sError;
 }