Skip to content

Instantly share code, notes, and snippets.

@thennequin
Created February 7, 2016 00:09
Show Gist options
  • Save thennequin/6cd782b3a5508255c794 to your computer and use it in GitHub Desktop.
Save thennequin/6cd782b3a5508255c794 to your computer and use it in GitHub Desktop.
ErrorHandler for PHP
<?php
class SimpleErrorHandler
{
static function Exception($msg, $level = 0)
{
//trigger_error("Member not exist"/*, E_USER_ERROR*/);
$traces = debug_backtrace();
self::PrintError($msg,$traces[$level]["file"], $traces[$level]["line"]);
}
static function Fatal($msg, $level = 0)
{
self::Exception($msg, $level);
die();
}
static function PrintError($msg, $file, $line)
{
echo '<div style="'
. 'margin:10px; '
. 'padding:10px; '
. 'background:#FFC3A8; '
. 'border:5px solid red;'
. '">';
if (null != $msg)
{
echo $msg;
}
echo '<div style="'
. 'margin:5px; '
. 'padding:5px; '
. 'background:#FFD6A8; '
//. 'border:5px solid red;'
. '">';
$fileContent = highlight_string(file_get_contents($file), true);
$fileContent = explode("<br />", $fileContent);
$start = $line-10;
if ($start < 0) $start = 0;
$end = $line+10;
for ($i = $start; $i<$end && $i < count($fileContent); ++$i)
{
echo '<div style="float: left;clear: both;width: 25px;">';
echo ($i+1);
echo '</div>';
echo '<div style="float: left;';
if ($i == $line-1)
{
echo 'background:#FF8049; ';
}
echo '">';
echo $fileContent[$i];
echo "</div>";
}
echo '<div style="clear: both;"></div>';
echo '</div>';
echo '</div>';
}
static function EncodeToHtml($string)
{
return nl2br(str_replace(' ','&nbsp;&nbsp;&nbsp;&nbsp;', str_replace(' ', ' &nbsp;', htmlentities($string))));
}
static function GetFileLines($file, $start, $end)
{
$returned = "";
$lineIndex = 1;
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
if ($lineIndex >= $start && $lineIndex <= $end)
{
$returned .= $line;
}else if ($lineIndex > $end)
{
break;
}
$lineIndex += 1;
}
} else {
// error opening the file.
}
fclose($handle);
return $returned;
}
// Gestionnaire d'erreurs
public static function ErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// Ce code d'erreur n'est pas inclus dans error_reporting()
return;
}
$msg = "";
switch ($errno)
{
case E_USER_ERROR:
$msg .= "<b>Error</b> ";
break;
case E_USER_WARNING:
$msg .= "<b>Warning</b> ";
break;
case E_USER_NOTICE:
$msg .= "<b>Notice</b> ";
break;
default:
$msg .= "<b>Error unkwnown : </b>";
break;
}
$msg .= "[$errno] $errstr<br />";
$msg .= "in <b>$errfile</b> on line <b>$errline</b>";
//$msg .= "<br />PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
self::PrintError($msg, $errfile, $errline);
return true;
}
public static function ShutdownHandler()
{
$lasterror = error_get_last();
if (null != $lasterror)
{
$msg = "";
switch ($lasterror['type'])
{
case E_ERROR:
$msg .= "<b>Error</b> : ";
break;
case E_CORE_ERROR:
$msg .= "<b>Core error</b> : ";
break;
case E_COMPILE_ERROR:
$msg .= "<b>Compile error</b> : ";
break;
case E_USER_ERROR:
$msg .= "<b>User error</b> : ";
break;
case E_RECOVERABLE_ERROR:
$msg .= "<b>Recoverable error</b> : ";
break;
case E_CORE_WARNING:
$msg .= "<b>Core warning</b> : ";
break;
case E_COMPILE_WARNING:
$msg .= "<b>Compile warning</b> : ";
break;
case E_PARSE:
$msg .= "<b>Parse error</b> : ";
break;
default:
return;
}
$msg .= $lasterror['message'] . "<br />";
$msg .= "in <b>".$lasterror['file']."</b> on line <b>".$lasterror['line']."</b>";
self::PrintError($msg, $lasterror['file'], $lasterror['line']);
}
}
}
set_error_handler("SimpleErrorHandler::ErrorHandler");
register_shutdown_function("SimpleErrorHandler::ShutdownHandler");
?>
@thennequin
Copy link
Author

thennequin commented Feb 7, 2016

How to use it

Just include this file.

Exemple

simpleerrorhandling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment