Source code for file /joomla/utilities/error.php
Documentation is available at error.php
* @version $Id: error.php 6762 2007-03-03 08:37:41Z tcp $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
// Import library dependencies
define('JERR_PHP5', version_compare(phpversion(), '5') >=
0);
// Error Definition: Illegal Options
define( 'JERR_ILLEGAL_OPTIONS', 1 );
// Error Definition: Callback does not exist
define( 'JERR_CALLBACK_NOT_CALLABLE', 2 );
// Error Definition: Illegal Handler
define( 'JERR_ILLEGAL_MODE', 3 );
$GLOBALS['_JError_Stack'] =
array();
* Default available error levels
$GLOBALS['_JError_Levels'] =
array( E_NOTICE =>
'Notice', E_WARNING =>
'Warning', E_ERROR =>
'Error' );
$GLOBALS['_JError_Handlers'] =
array( E_NOTICE =>
array( 'mode' =>
'message' ), E_WARNING =>
array( 'mode' =>
'message' ), E_ERROR =>
array( 'mode' =>
'callback', 'options' =>
array('JError','customErrorPage') ) );
* This class is inspired in design and concept by patErrorManager <http://www.php-tools.net>
* patErrorManager contributors include:
* @package Joomla.Framework
* Method to determine if a value is an exception object. This check supports both JException and PHP5 Exception objects
* @param mixed &$object Object to check
* @return boolean True if argument is an exception, false otherwise.
// supports PHP 5 exception handling
return is_a($object, 'JException') |
is_a($object, 'Exception');
* Method for retrieving the last exception object in the error stack
* @return mixed Last exception object in the error stack or boolean false if none exist
if (!isset
($GLOBALS['_JError_Stack'][0])) {
$error =
&$GLOBALS['_JError_Stack'][0];
* Method for retrieving the exception stack
* @return array Chronological array of errors that have been stored during script execution
return $GLOBALS['_JError_Stack'];
* Create a new JException object given the passed arguments
* @param int $level The error level - use any of PHP's own error levels for this: E_ERROR, E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE.
* @param string $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
* @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
* @return mixed The JException object
function & raise($level, $code, $msg, $info =
null, $backtrace =
false)
$error =
new JException($level, $code, $msg, $info, $backtrace);
// see what to do with this kind of error
$function =
'handle'.
ucfirst($handler['mode']);
$reference =
& JError::$function ($error, (isset
($handler['options'])) ?
$handler['options'] :
array());
// This is required to prevent a very unhelpful white-screen-of-death
'JError::raise -> Static method JError::' .
$function .
' does not exist.' .
' Contact a developer to debug' .
'<br/><strong>Error was</strong> ' .
'<br/>' .
$error->get('message')
//store and return the error
$GLOBALS['_JError_Stack'][] =
& $reference;
* Wrapper method for the {@link raise()} method with predefined error level of E_ERROR and backtrace set to true.
* @param string $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
* @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
* @return object $error The configured JError object
$reference =
& JError::raise(E_ERROR, $code, $msg, $info, true);
* Wrapper method for the {@link raise()} method with predefined error level of E_WARNING and backtrace set to false.
* @param string $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
* @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
* @return object $error The configured JError object
$reference =
& JError::raise(E_WARNING, $code, $msg, $info);
* Wrapper method for the {@link raise()} method with predefined error level of E_NOTICE and backtrace set to false.
* @param string $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
* @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
* @return object $error The configured JError object
$reference =
& JError::raise(E_NOTICE, $code, $msg, $info);
* Method to get the current error handler settings for a specified error level.
* @param int $level The error level to retrieve. This can be any of PHP's own error levels, e.g. E_ALL, E_NOTICE...
* @return array All error handling details
return $GLOBALS['_JError_Handlers'][$level];
* Method to set the way the JError will handle different error levels. Use this if you want to override the default settings.
* You may also set the error handling for several modes at once using PHP's bit operations.
* - E_ALL = Set the handling for all levels
* - E_ERROR | E_WARNING = Set the handling for errors and warnings
* - E_ALL ^ E_ERROR = Set the handling for all levels except errors
* @param int $level The error level for which to set the error handling
* @param string $mode The mode to use for the error handling.
* @param mixed $options Optional: Any options needed for the given mode.
* @return mixed True on success, or a JException object if failed.
$levels =
$GLOBALS['_JError_Levels'];
$function =
'handle'.
ucfirst($mode);
foreach ($levels as $eLevel =>
$eTitle) {
if (($level & $eLevel) !=
$eLevel) {
if ($mode ==
'callback') {
$GLOBALS['_JError_Handlers'][$eLevel] =
array ('mode' =>
$mode);
$GLOBALS['_JError_Handlers'][$eLevel]['options'] =
$options;
* Method to register a new error level for handling errors
* This allows you to add custom error levels to the built-in
* @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
if( isset
($GLOBALS['_JError_Levels'][$level]) ) {
$GLOBALS['_JError_Levels'][$level] =
$name;
* Translate an error level integer to a human readable string
* e.g. E_ERROR will be translated to 'Error'
* @param int $level Error level to translate
* @return mixed Human readable error level name or boolean false if it doesn't exist
if( isset
($GLOBALS['_JError_Levels'][$level]) ) {
return $GLOBALS['_JError_Levels'][$level];
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* - Echos the error message to output
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
if (isset
($_SERVER['HTTP_HOST'])) {
echo
"<br /><b>jos-$level_human</b>: ".
$error->get('message').
"<br />\n";
fwrite(STDERR, "jos-$level_human: ".
$error->get('message').
"\n");
echo
"jos-$level_human: ".
$error->get('message').
"\n";
* - Echos the error message to output as well as related info
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
$info =
$error->get('info');
if (isset
($_SERVER['HTTP_HOST'])) {
echo
"<br /><b>J$level_human</b>: ".
$error->get('message').
"<br />\n";
echo
" ".
$info.
"<br />\n";
echo
$error->getBacktrace(true);
echo
"J$level_human: ".
$error->get('message').
"\n";
* - Echos the error message to output and then dies
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
if (isset
($_SERVER['HTTP_HOST'])) {
die("<br /><b>J$level_human</b> ".
$error->get('message').
"<br />\n");
fwrite(STDERR, "J$level_human ".
$error->get('message').
"\n");
die("J$level_human ".
$error->get('message').
"\n");
* - Enqueues the error message into the system queue
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
$mainframe->enqueueMessage($error->get('message'), 'error');
* - Logs the error message to a system log file
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
$fileName =
date('Y-m-d').
'.error.log';
$options['format'] =
"{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}";
$entry['level'] =
$error->get('level');
$entry['code'] =
$error->get('code');
$entry['message'] =
str_replace(array ("\r","\n"), array ('','\\n'), $error->get('message'));
* - Triggers a PHP native error with the error message
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
switch( $error->get('level') )
* - Send the error object to a callback method for error handling
* @param object $error Exception object to handle
* @param array $options Handler options
* @return object The exception object
* Display a custom error page and exit gracefully
* @param object $error Exception object
jimport('joomla.document.document');
// Get the current template from the application
$template =
$mainframe->getTemplate();
// Push the error object into the document
$document->setError($error);
$document->setTitle(JText::_('Error').
': '.
$error->code);
$data =
$document->render(false, array (
'directory' =>
JPATH_BASE.
DS.
'templates',
'debug' =>
$config->getValue('config.debug')
* Joomla! Exception object.
* This class is inspired in design and concept by patError <http://www.php-tools.net>
* patError contributors include:
* @package Joomla.Framework
* Additional info about the error relevant to the developer
* - e.g. if a database connect fails, the dsn used
* Name of the file the error occurred in [Available if backtrace is enabled]
* Line number the error occurred in [Available if backtrace is enabled]
* Name of the method the error occurred in [Available if backtrace is enabled]
* Name of the class the error occurred in [Available if backtrace is enabled]
* Arguments recieved by the method the error occurred in [Available if backtrace is enabled]
* - used to set up the error with all needed error details.
* @param int $level The error level (use the PHP constants E_ALL, E_NOTICE etc.).
* @param string $code The error code from the application
* @param string $msg The error message
* @param string $info Optional: The additional error information.
function __construct( $level, $code, $msg, $info =
null, $backtrace =
false )
if( isset
( $this->backtrace[$i]['function'] ) )
if( isset
( $this->backtrace[$i]['args'] ) ) {
* Method to get the backtrace information for an exception object
* @return array backtrace
if (isset
($back['file']) &&
strpos($back['file'], 'error.php') ===
false) {
$result .=
'<br />'.
$back['file'].
':'.
$back['line'];