10 defined(
'JPATH_PLATFORM') or die;
46 public static $legacy =
false;
54 protected static $levels = array(E_NOTICE =>
'Notice', E_WARNING =>
'Warning', E_ERROR =>
'Error');
61 protected static $handlers = array(
62 E_NOTICE => array(
'mode' =>
'ignore'),
63 E_WARNING => array(
'mode' =>
'ignore'),
64 E_ERROR => array(
'mode' =>
'ignore')
73 protected static $stack = array();
85 public static function isError($object)
89 return $object instanceof Exception;
102 public static function getError($unset =
false)
106 if (!isset(self::$stack[0]))
113 $error = array_shift(self::$stack);
117 $error = &self::$stack[0];
130 public static function getErrors()
151 self::$stack[] = &$e;
173 public static function raise($level, $code, $msg, $info = null, $backtrace =
false)
178 $exception =
new JException($msg, $code, $level, $info, $backtrace);
179 return self::throwError($exception);
193 public static function throwError(&$exception)
197 static $thrown =
false;
202 self::handleEcho($exception, array());
209 $level = $exception->get(
'level');
212 $handler = self::getErrorHandling($level);
214 $function =
'handle' . ucfirst($handler[
'mode']);
215 if (is_callable(array(
'JError', $function)))
217 $reference = call_user_func_array(array(
'JError', $function), array(&$exception, (isset($handler[
'options'])) ? $handler[
'options'] : array()));
223 'JError::raise -> Static method JError::' . $function .
' does not exist. Contact a developer to debug' .
224 '<br /><strong>Error was</strong> <br />' . $exception->getMessage()
249 public static function raiseError($code, $msg, $info = null)
253 return self::raise(E_ERROR, $code, $msg, $info,
true);
272 public static function raiseWarning($code, $msg, $info = null)
276 return self::raise(E_WARNING, $code, $msg, $info);
295 public static function raiseNotice($code, $msg, $info = null)
299 return self::raise(E_NOTICE, $code, $msg, $info);
313 public static function getErrorHandling($level)
317 return self::$handlers[$level];
347 public static function setErrorHandling($level, $mode, $options = null)
351 $levels = self::$levels;
353 $function =
'handle' . ucfirst($mode);
355 if (!is_callable(array(
'JError', $function)))
357 return self::raiseError(E_ERROR,
'JError:' .
JERROR_ILLEGAL_MODE,
'Error Handling mode is not known',
'Mode: ' . $mode .
' is not implemented.');
360 foreach ($levels as $eLevel => $eTitle)
362 if (($level & $eLevel) != $eLevel)
368 if ($mode ==
'callback')
370 if (!is_array($options))
375 if (!is_callable($options))
377 $tmp = array(
'GLOBAL');
378 if (is_array($options))
380 $tmp[0] = $options[0];
381 $tmp[1] = $options[1];
388 return self::raiseError(
391 'Function is not callable',
392 'Function:' . $tmp[1] .
' scope ' . $tmp[0] .
'.'
398 self::$handlers[$eLevel] = array(
'mode' => $mode);
399 if ($options != null)
401 self::$handlers[$eLevel][
'options'] = $options;
417 public static function attachHandler()
421 set_error_handler(array(
'JError',
'customErrorHandler'));
433 public static function detachHandler()
437 restore_error_handler();
457 public static function registerErrorLevel($level, $name, $handler =
'ignore')
461 if (isset(self::$levels[$level]))
466 self::$levels[$level] = $name;
467 self::setErrorHandling($level, $handler);
484 public static function translateErrorLevel($level)
488 if (isset(self::$levels[$level]))
490 return self::$levels[$level];
509 public static function handleIgnore(&$error, $options)
529 public static function handleEcho(&$error, $options)
533 $level_human = self::translateErrorLevel($error->get(
'level'));
536 if (defined(
'JDEBUG'))
538 $backtrace = $error->getTrace();
540 for ($i = count($backtrace) - 1; $i >= 0; $i--)
542 if (isset($backtrace[$i][
'class']))
544 $trace .= sprintf(
"\n%s %s %s()", $backtrace[$i][
'class'], $backtrace[$i][
'type'], $backtrace[$i][
'function']);
548 $trace .= sprintf(
"\n%s()", $backtrace[$i][
'function']);
551 if (isset($backtrace[$i][
'file']))
553 $trace .= sprintf(
' @ %s:%d', $backtrace[$i][
'file'], $backtrace[$i][
'line']);
558 if (isset($_SERVER[
'HTTP_HOST']))
561 echo
"<br /><b>jos-$level_human</b>: "
562 . $error->get(
'message') .
"<br />\n"
563 . (defined(
'JDEBUG') ? nl2br($trace) :
'');
568 if (defined(
'STDERR'))
570 fwrite(STDERR,
"J$level_human: " . $error->get(
'message') .
"\n");
571 if (defined(
'JDEBUG'))
573 fwrite(STDERR, $trace);
578 echo
"J$level_human: " . $error->get(
'message') .
"\n";
579 if (defined(
'JDEBUG'))
602 public static function handleVerbose(&$error, $options)
606 $level_human = self::translateErrorLevel($error->get(
'level'));
607 $info = $error->get(
'info');
609 if (isset($_SERVER[
'HTTP_HOST']))
612 echo
"<br /><b>J$level_human</b>: " . $error->get(
'message') .
"<br />\n";
616 echo
"   " . $info .
"<br />\n";
619 echo $error->getBacktrace(
true);
624 echo
"J$level_human: " . $error->get(
'message') .
"\n";
627 echo
"\t" . $info .
"\n";
648 public static function handleDie(&$error, $options)
652 $level_human = self::translateErrorLevel($error->get(
'level'));
654 if (isset($_SERVER[
'HTTP_HOST']))
657 jexit(
"<br /><b>J$level_human</b>: " . $error->get(
'message') .
"<br />\n");
662 if (defined(
'STDERR'))
664 fwrite(STDERR,
"J$level_human: " . $error->get(
'message') .
"\n");
669 jexit(
"J$level_human: " . $error->get(
'message') .
"\n");
689 public static function handleMessage(&$error, $options)
694 $type = ($error->get(
'level') == E_NOTICE) ?
'notice' :
'error';
695 $appl->enqueueMessage($error->get(
'message'), $type);
713 public static function handleLog(&$error, $options)
721 $options[
'text_file'] = date(
'Y-m-d') .
'.error.log';
722 $options[
'format'] =
"{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}";
727 str_replace(array(
"\r",
"\n"), array(
'',
'\\n'), $error->get(
'message')),
728 $error->get(
'level'),
731 $entry->code = $error->get(
'code');
750 public static function handleCallback(&$error, $options)
754 return call_user_func($options, $error);
767 public static function customErrorPage(&$error)
778 $template = $app->getTemplate();
781 $document->setError($error);
790 $document->setTitle(
JText::_(
'Error') .
': ' . $error->getCode());
791 $data = $document->render(
false, array(
'template' => $template,
'directory' => JPATH_THEMES,
'debug' => $config->get(
'debug')));
796 self::handleEcho($error, array());
801 $app->allowCache(
false);
803 $app->setBody($data);
804 echo $app->toString();
811 self::handleEcho($error, array());
829 public static function customErrorHandler($level, $msg)
833 self::raise($level,
'', $msg);
846 public static function renderBacktrace($error)
851 $backtrace = $error->getTrace();
853 if (is_array($backtrace))
857 echo
'<table cellpadding="0" cellspacing="0" class="Table">';
859 echo
' <td colspan="3" class="TD"><strong>Call stack</strong></td>';
862 echo
' <td class="TD"><strong>#</strong></td>';
863 echo
' <td class="TD"><strong>Function</strong></td>';
864 echo
' <td class="TD"><strong>Location</strong></td>';
867 for ($i = count($backtrace) - 1; $i >= 0; $i--)
870 echo
' <td class="TD">' . $j .
'</td>';
872 if (isset($backtrace[$i][
'class']))
874 echo
' <td class="TD">' . $backtrace[$i][
'class'] . $backtrace[$i][
'type'] . $backtrace[$i][
'function'] .
'()</td>';
878 echo
' <td class="TD">' . $backtrace[$i][
'function'] .
'()</td>';
881 if (isset($backtrace[$i][
'file']))
883 echo
' <td class="TD">' . $backtrace[$i][
'file'] .
':' . $backtrace[$i][
'line'] .
'</td>';
887 echo
' <td class="TD"> </td>';
895 $contents = ob_get_contents();