note 116502 deleted from function.set-error-handler by cmb
| From: | cmb@php.net | Date: | Sun, 13 Oct 2019 07:43:11 +0000 |
| Subject: | note 116502 deleted from function.set-error-handler by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212601@lists.php.net to get a copy of this message | ||
Note Submitter: yvan.burrie
----
Here's a way to customize error messages for each function or functions within classes:
<?php
// This lists all error messages from each function including functions contained within classes and
their associated scope:
$_ERRORS [ 'errorTest::makeError1' ]= 'Attempting to make an error' ;
$_ERRORS [ 'errorTest->makeError2' ]= 'Attempting to make another error' ;
$_ERRORS [ 'strpos' ] = 'Failed to find offset of data from string' ;
$_ERRORS [ 'strlen' ] = 'Failed to calculate length of string' ;
$_ERRORS [ 'substr' ] = 'Failed to retrieve data from string' ;
$_ERRORS [ 'pack' ] = 'Failed to export binary package' ;
$_ERRORS [ 'unpack' ] = 'Failed to import binary package' ;
$_ERRORS [ 'number_format' ] = 'Failed to represent numeracy as number' ;
$_ERRORS [ 'implode' ] = 'Failed to implode array elements' ;
$_ERRORS [ 'explode' ] = 'Failed to explode array elements' ;
$_ERRORS [ 'fopen' ] = 'Failed to initialize stream opening' ;
$_ERRORS [ 'fread' ] = 'Failed to extract data from stream' ;
$_ERRORS [ 'fwrite' ] = 'Failed to retract data into stream' ;
$_ERRORS [ 'fseek' ] = 'Failed to seek offset from stream' ;
$_ERRORS [ 'rewind' ] = 'Failed to rewind offset from stream' ;
$_ERRORS [ 'ftell' ] = 'Failed to retrieve offset from stream' ;
$_ERRORS [ 'file_get_contents' ] = 'Failed to retrieve contents from file' ;
$_ERRORS [ 'file_put_contents' ] = 'Failed to conduct contents into file' ;
$_ERRORS [ 'stream_get_contents' ] = 'Failed to retrieve contents from stream' ;
$_ERRORS [ 'call_user_func_array' ] = 'Failed to call function with array' ;
$_ERRORS [ 'max' ] = 'Failed to retrieve maximum elements' ;
$_ERRORS [ 'min' ] = 'Failed to retrieve minimum elements' ;
$_ERRORS [ 'array_chunk' ] = 'Failed to retrieve chunks of array elements' ;
$_ERRORS [ 'array_combine' ] = 'Failed to combine keys for array elements' ;
$_ERRORS [ 'array_diff' ] =
$_ERRORS [ 'array_diff_assoc' ] =
$_ERRORS [ 'array_diff_key' ] =
$_ERRORS [ 'array_diff_uassoc' ] =
$_ERRORS [ 'array_diff_ukey' ] = 'Failed to differentiate array elements' ;
$_ERRORS [ 'array_fill' ] =
$_ERRORS [ 'array_fill_keys' ] = 'Failed to allocate array elements' ;
$_ERRORS [ 'array_flip' ] = 'Failed to flip array elements to keys' ;
//$_ERRORS [ 'array_slice' ] = 'Failed to slice array elements' ;
/**
*
*/
set_error_handler
(
function ( $code , $text , $file , $line , array $cont ) use ( $_ERRORS )
{
if ( error_reporting () )
{
$result = [] ;
foreach ( debug_backtrace () as $detail )
{
$string = @ $_ERRORS [ @ $detail [ 'class' ] . @ $detail [ 'type' ] . @
$detail [ 'function' ] ] ;
if ( isset ( $string ) )
{
$result [] = $string ;
}
}
if ( count ( $result ) )
{
$result = implode ( ': ' , array_reverse ( $result ) ) . '.' ;
}
else
{
$result = 'Undefined procedural behaviour' ;
}
throw new ErrorException ( $result , $code , 0 , $file , $line ) ;
}
} ,
E_ALL | E_STRICT
) ;
// Examples :
class errorTest
{
static function makeError1 ()
{
strpos () ;
}
public function makeError2 ()
{
strlen () ;
}
}
$err = new errorTest ;
try
{
substr () ;
}
catch ( Exception $E )
{
echo $E -> getMessage () . "\n" ;
}
try
{
errorTest :: makeError1 () ;
}
catch ( Exception $E )
{
echo $E -> getMessage () . "\n" ;
}
try
{
$err -> makeError2 () ;
}
catch ( Exception $E )
{
echo $E -> getMessage () . "\n" ;
}
try
{
// this function was not listed in the $_ERRORS variable and will therefore view the default
message
array_slice () ;
}
catch ( Exception $E )
{
echo $E -> getMessage () . "\n" ;
}
/*
The above examples will output:
Failed to retrieve data from string.
Attempting to make an error: Failed to find offset of data from string.
Attempting to make another error: Failed to calculate length of string.
Undefined procedural behaviour
*/
?>