note 85646 deleted from class.errorexception by googleguy
| From: | googleguy@php.net | Date: | Wed, 12 Sep 2012 03:40:20 +0000 |
| Subject: | note 85646 deleted from class.errorexception by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190636@lists.php.net to get a copy of this message | ||
Note Submitter: makariverslund at gmail dot com
----
Building upon the notes present, here is a set of functions that transforms the error into an
exception and reports the error to screen / log depending upon the PHP ini settings.
//Begin helper function definition
function ReportError ($msg)
{
// be sure that the supplied parameter is a string and not empty
if (empty ($msg) || !is_string ($msg))
{
throw new ErrorException ('Invalid parameter supplied to ReportError', 0, E_ERROR);
}
// retrieve error settings
$display = strtolower (ini_get ('display_errors'));
$log = strtolower (ini_get ('log_errors'));
// check if we're displaying errors
if ($display === 'on' || $display === '1' || $display === 1 || $display ===
'true' || $display === true)
{
echo $msg;
}
// check if we're logging errors
if ($log === 'on' || $log === '1' || $log === 1 || $log === 'true' ||
$log === true)
{
$result = error_log ($msg);
// check for error while logging
if (!$result)
{
throw new ErrorException ('Attempt to write message to error log failed in
ReportError', 0, E_ERROR);
}
}
}
//Begin main function definition
function ErrorsAsExceptions ($level, $msg, $fileName, $lineNumber)
{
// do nothing if error reporting is turned off
if (error_reporting () === 0)
{
return;
}
// be sure received error is supposed to be reported
if (error_reporting () & $level)
{
try
{
// report error to appropriate channels
ReportError ($reportMsg);
}
catch (ErrorException $e)
{
// ignore errors while reporting
}
// go ahead and throw the exception
throw new ErrorException ($msg, 0, $level, $fileName, $lineNumber);
}
}