Source code for file /pattemplate/patErrorManager.php
Documentation is available at patErrorManager.php
* patErrorManager main error management class used by pat tools for the
* application-internal error management. Creates patError objects for
* any errors for precise error management.
* $Id: patErrorManager.php 47 2005-09-15 02:55:27Z rhuk $
* error definition: illegal options.
define( 'PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS', 1 );
* error definition: callback function does not exist.
define( 'PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE', 2 );
* error definition: illegal error handling mode.
define( 'PATERRORMANAGER_ERROR_ILLEGAL_MODE', 3 );
* global definitions needed to keep track of things when calling the patErrorManager
$GLOBALS['_pat_errorHandling'] =
array(
E_NOTICE =>
array( 'mode' =>
'echo' ),
E_WARNING =>
array( 'mode' =>
'echo' ),
E_ERROR =>
array( 'mode' =>
'die' )
* Stored in a variable to keep them flexible
$GLOBALS['_pat_errorLevels'] =
array(
* Stored in a variable allows to change during runtime
$GLOBALS['_pat_errorClass'] =
'patError';
* Store error-codes that will be ignored forever
$GLOBALS['_pat_errorIgnores'] =
array();
* Store error-codes that will be ignored once
$GLOBALS['_pat_errorExpects'] =
array();
* patErrorManager main error management class used by pat tools for the
* application-internal error management. Creates patError objects for
* any errors for precise error management.
* @link http://www.php-tools.net
* @todo implement ignoreError() to ignore errrors with a certain code
* @todo implement expectError() to ignore an error with a certain code only once.
* method for checking whether the return value of a pat application method is a pat
* @return boolean $result True if argument is a patError-object, false otherwise.
* wrapper for the {@link raise()} method where you do not have to specify the
* error level - a {@link patError} object with error level E_ERROR will be returned.
* @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 patError object
* wrapper for the {@link raise()} method where you do not have to specify the
* error level - a {@link patError} object with error level E_WARNING will be returned.
* @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 patError object
* wrapper for the {@link raise()} method where you do not have to specify the
* error level - a {@link patError} object with error level E_NOTICE will be returned.
* @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 patError object
* creates a new patError object given the specified information.
* @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 $error The configured patError object or false if this error should be ignored
* @todo implement 'simple' mode that returns just false (BC for patConfiguration)
* @todo either remove HTML tags and entities from output or test for enviroment!!! <b></b> in shell is ugly!
function &raise( $level, $code, $msg, $info =
null )
if( in_array( $code, $GLOBALS['_pat_errorIgnores'] ) )
// this error was expected
if( !empty( $GLOBALS['_pat_errorExpects'] ) )
$expected =
array_pop( $GLOBALS['_pat_errorExpects'] );
$class =
$GLOBALS['_pat_errorClass'];
include_once dirname( __FILE__
) .
'/'.
$class .
'.php';
$error =
& new $class( $level, $code, $msg, $info );
// see what to do with this kind of error
$function =
'handleError' .
ucfirst( $handling['mode'] );
if (is_callable( array( 'patErrorManager', $function ) )) {
// 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->getMessage()
* register a new error level
* This allows you to add custom error levels to the built-in
* You may use this level in subsequent calls to raise().
* Error handling will be set to 'ignore' for the new level, you
* may change it by using setErrorHandling().
* You could be using PHP's predefined constants for error levels
* or any other integer value.
* @param integer error level
* @param string human-readable name
* @return boolean true on success; false if the level already has been registered
* @see raise(), setErrorHandling()
* @link http://www.php.net/manual/en/function.error-reporting.php
if( isset
( $GLOBALS['_pat_errorLevels'][$level] ) )
$GLOBALS['_pat_errorLevels'][$level] =
$name;
* sets the way the patErrorManager will handle teh 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 $result True on success, or a patError object if failed.
* @see getErrorHandling()
$levels =
$GLOBALS['_pat_errorLevels'];
$function =
'handleError' .
ucfirst( $mode );
if( !is_callable( array( 'patErrorManager', $function ) ) )
'Error Handling mode is not knwon',
'Mode: ' .
$mode .
' is not implemented.'
foreach( $levels as $eLevel =>
$eTitle )
if( ( $level & $eLevel ) !=
$eLevel )
if( $mode ==
'callback' )
'Options for callback not valid'
$tmp =
array( 'GLOBAL' );
'Function is not callable',
'Function:' .
$tmp[1] .
' scope ' .
$tmp[0] .
'.'
$GLOBALS['_pat_errorHandling'][$eLevel] =
array( 'mode' =>
$mode );
$GLOBALS['_pat_errorHandling'][$eLevel]['options'] =
$options;
* retrieves the current error handling settings for the 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 $handling All error handling details
return $GLOBALS['_pat_errorHandling'][$level];
* translate an error level
* returns the human-readable name for an error level,
* e.g. E_ERROR will be translated to 'Error'.
* @param integer error level
* @return string human-readable representation
if( isset
( $GLOBALS['_pat_errorLevels'][$level] ) )
return $GLOBALS['_pat_errorLevels'][$level];
return 'Unknown error level';
* In order to autoload this class, the filename containing that class must be
* named like the class itself; with an appending ".php". Although the file must be stored
* in the same directory as patErrorManager.php (this file)
* @param string $name classname
* @return boolean $result true on success
// include old error-class
if( $name !==
$GLOBALS['_pat_errorClass'] &&
!class_exists( $GLOBALS['_pat_errorClass'] ) )
include_once dirname( __FILE__
) .
'/' .
$GLOBALS['_pat_errorClass'] .
'.php';
$GLOBALS['_pat_errorClass'] =
$name;
* add error codes to be ingored
* @param mixed $codes either an array of error code or a single code that will be ignored in future
* @return boolean $result true on success
$codes =
array( $codes );
$codes =
array_merge( $GLOBALS['_pat_errorIgnores'], $codes );
* @return boolean $result true on success
$codes =
array( $codes );
foreach( $codes as $code )
$index =
array_search( $code, $GLOBALS['_pat_errorIgnores'] );
unset
( $GLOBALS['_pat_errorIgnores'][$index] );
$GLOBALS['_pat_errorIgnores'] =
array_values( $GLOBALS['_pat_errorIgnores'] );
* recieve all registerd error codes that will be ignored
* @return array $codes list of error codes
return $GLOBALS['_pat_errorIgnores'];
* empty list of errors to be ignored
* @return boolean $result true on success
$GLOBALS['_pat_errorIgnores'] =
array();
* add expected errors to stack
* @param mixed $codes either an array of error code or a single code that will be ignored in future
* @return boolean $result true on success
$codes =
array( $codes );
array_push( $GLOBALS['_pat_errorExpects'], $codes );
* remove top of error-codes from stack
* @return boolean $result true on success
if( empty( $GLOBALS['_pat_errorExpects'] ) )
* recieve all registerd error codes that will be ignored
* @return array $codes list of error codes
return $GLOBALS['_pat_errorExpects'];
* empty list of errors to be ignored
* @return boolean $result true on success
$GLOBALS['_pat_errorExpects'] =
array();
* @param object $error patError-Object
* @param array $options options for handler
* @return object $error error-object
function &handleErrorIgnore( &$error, $options )
* @param object $error patError-Object
* @param array $options options for handler
* @return object $error error-object
function &handleErrorEcho( &$error, $options )
if( isset
( $_SERVER['HTTP_HOST'] ) )
echo
"<br /><b>pat-$level_human</b>: " .
$error->getMessage() .
"<br />\n";
fwrite( STDERR, "pat-$level_human: " .
$error->getMessage() .
"\n" );
echo
"pat-$level_human: " .
$error->getMessage() .
"\n";
* display verbose output for developing purpose
* @param object $error patError-Object
* @param array $options options for handler
* @return object $error error-object
function &handleErrorVerbose( &$error, $options )
$info =
$error->getInfo();
if( isset
( $_SERVER['HTTP_HOST'] ) )
echo
"<br /><b>pat-$level_human</b>: " .
$error->getMessage() .
"<br />\n";
echo
" " .
$error->getInfo() .
"<br />\n";
echo
$error->getBacktrace( true );
echo
"pat-$level_human: " .
$error->getMessage() .
"\n";
echo
" " .
$error->getInfo() .
"\n";
* display error-message and die
* @param object $error patError-Object
* @param array $options options for handler
* @return object $error error-object
function &handleErrorDie( &$error, $options )
if( isset
( $_SERVER['HTTP_HOST'] ) )
die( "<br /><b>pat-$level_human</b> " .
$error->getMessage() .
"<br />\n" );
fwrite( STDERR, "pat-$level_human " .
$error->getMessage() .
"\n" );
die( "pat-$level_human " .
$error->getMessage() .
"\n" );
* @param object $error patError-Object
* @param array $options options for handler
* @return object $error error-object
function &handleErrorTrigger( &$error, $options )
switch( $error->getLevel() )
* forward error to custom handler
* @param object $error patError-Object
* @param array $options options for handler
* @return object $error error-object
function &handleErrorCallback( &$error, $options )
$opt =
$options['options'];