error.php
来自「国外免费开源的内容管理系统」· PHP 代码 · 共 575 行 · 第 1/2 页
PHP
575 行
/**
* Method that attaches the error handler to JError
*
* @access public
* @see set_error_handler
*/
function attachHandler()
{
set_error_handler(array('JError', 'customErrorHandler'));
}
/**
* Method that dettaches the error handler from JError
*
* @access public
* @see restore_error_handler
*/
function detachHandler()
{
restore_error_handler();
}
/**
* Method to register a new error level for handling errors
*
* This allows you to add custom error levels to the built-in
* - E_NOTICE
* - E_WARNING
* - E_NOTICE
*
* @static
* @param int $level Error level to register
* @param string $name Human readable name for the error level
* @param string $handler Error handler to set for the new error level [optional]
* @return boolean True on success; false if the level already has been registered
* @since 1.5
*/
function registerErrorLevel( $level, $name, $handler = 'ignore' )
{
if( isset($GLOBALS['_JERROR_LEVELS'][$level]) ) {
return false;
}
$GLOBALS['_JERROR_LEVELS'][$level] = $name;
JError::setErrorHandling($level, $handler);
return true;
}
/**
* Translate an error level integer to a human readable string
* e.g. E_ERROR will be translated to 'Error'
*
* @static
* @param int $level Error level to translate
* @return mixed Human readable error level name or boolean false if it doesn't exist
* @since 1.5
*/
function translateErrorLevel( $level )
{
if( isset($GLOBALS['_JERROR_LEVELS'][$level]) ) {
return $GLOBALS['_JERROR_LEVELS'][$level];
}
return false;
}
/**
* Ignore error handler
* - Ignores the error
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function & handleIgnore(&$error, $options)
{
return $error;
}
/**
* Echo error handler
* - Echos the error message to output
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function & handleEcho(&$error, $options)
{
$level_human = JError::translateErrorLevel($error->get('level'));
if (isset ($_SERVER['HTTP_HOST'])) {
// output as html
echo "<br /><b>jos-$level_human</b>: ".$error->get('message')."<br />\n";
} else {
// output as simple text
if (defined('STDERR')) {
fwrite(STDERR, "J$level_human: ".$error->get('message')."\n");
} else {
echo "J$level_human: ".$error->get('message')."\n";
}
}
return $error;
}
/**
* Verbose error handler
* - Echos the error message to output as well as related info
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function & handleVerbose(& $error, $options)
{
$level_human = JError::translateErrorLevel($error->get('level'));
$info = $error->get('info');
if (isset ($_SERVER['HTTP_HOST'])) {
// output as html
echo "<br /><b>J$level_human</b>: ".$error->get('message')."<br />\n";
if ($info != null) {
echo " ".$info."<br />\n";
}
echo $error->getBacktrace(true);
} else {
// output as simple text
echo "J$level_human: ".$error->get('message')."\n";
if ($info != null) {
echo "\t".$info."\n";
}
}
return $error;
}
/**
* Die error handler
* - Echos the error message to output and then dies
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function & handleDie(& $error, $options)
{
$level_human = JError::translateErrorLevel($error->get('level'));
if (isset ($_SERVER['HTTP_HOST'])) {
// output as html
jexit("<br /><b>J$level_human</b> ".$error->get('message')."<br />\n");
} else {
// output as simple text
if (defined('STDERR')) {
fwrite(STDERR, "J$level_human ".$error->get('message')."\n");
} else {
jexit("J$level_human ".$error->get('message')."\n");
}
}
return $error;
}
/**
* Message error handler
* - Enqueues the error message into the system queue
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function & handleMessage(& $error, $options)
{
global $mainframe;
$type = ($error->get('level') == E_NOTICE) ? 'notice' : 'error';
$mainframe->enqueueMessage($error->get('message'), $type);
return $error;
}
/**
* Log error handler
* - Logs the error message to a system log file
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function & handleLog(& $error, $options)
{
static $log;
if ($log == null)
{
jimport('joomla.error.log');
$fileName = date('Y-m-d').'.error.log';
$options['format'] = "{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}";
$log = & JLog::getInstance($fileName, $options);
}
$entry['level'] = $error->get('level');
$entry['code'] = $error->get('code');
$entry['message'] = str_replace(array ("\r","\n"), array ('','\\n'), $error->get('message'));
$log->addEntry($entry);
return $error;
}
/**
* Callback error handler
* - Send the error object to a callback method for error handling
*
* @static
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* @since 1.5
*
* @see raise()
*/
function &handleCallback( &$error, $options )
{
$result = call_user_func( $options, $error );
return $result;
}
/**
* Display a custom error page and exit gracefully
*
* @static
* @param object $error Exception object
* @return void
* @since 1.5
*/
function customErrorPage(& $error)
{
// Initialize variables
jimport('joomla.document.document');
$app = & JFactory::getApplication();
$document = & JDocument::getInstance('error');
$config = & JFactory::getConfig();
// Get the current template from the application
$template = $app->getTemplate();
// Push the error object into the document
$document->setError($error);
@ob_end_clean();
$document->setTitle(JText::_('Error').': '.$error->code);
$data = $document->render(false, array (
'template' => $template,
'directory' => JPATH_THEMES,
'debug' => $config->getValue('config.debug')
));
JResponse::setBody($data);
echo JResponse::toString();
$app->close(0);
}
function customErrorHandler($level, $msg)
{
JError::raise($level, '', $msg);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?