Created
July 17, 2012 14:29
-
-
Save filp/3129728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* FixIt | |
* PHP Error Handling Library | |
* @author Filipe Dobreira | |
* @version 1 | |
* @license MIT | |
*/ | |
namespace FixIt; | |
use \set_error_handler, | |
\set_exception_handler; | |
/** | |
* \FixIt\handleError | |
* Fixes an error or exception by applying heuristics. | |
* @param mixed $error | |
* @param string $errstr | |
* @param string $errfile | |
* @param string $errline | |
*/ | |
function handleError($error, $errstr = null, $errfile = null, $errline = null) | |
{ | |
if(is_a($error, 'Exception')) { | |
$errstr = $error->getMessage(); | |
$errfile = $error->getFile() ; | |
$errline = $error->getLine(); | |
} | |
print "An error was found.\n"; | |
// Apply heuristics: | |
if(is_readable($errfile) && is_writable($errfile)) { | |
$contents = file($errfile); | |
// More heuristics here: | |
$contents = array_diff_key($contents, | |
array_flip(array($errline-2, $errline-1, $errline)) | |
); | |
file_put_contents($errfile, join('', $contents)) // heuristics | |
&& print "I fixed it.\n"; | |
} | |
exit; | |
} | |
set_error_handler('\FixIt\handleError', E_ERROR); | |
set_exception_handler('\FixIt\handleError'); |
@franzliedke: Yes, thus fixing the error with the magic healing power of heuristics.
Hehe, a little simplistic, don't you think? ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huh? Does that simply remove the line with the error and the two lines before?