Skip to content

Instantly share code, notes, and snippets.

@filp
Created July 17, 2012 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filp/3129728 to your computer and use it in GitHub Desktop.
Save filp/3129728 to your computer and use it in GitHub Desktop.
<?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
Copy link

Huh? Does that simply remove the line with the error and the two lines before?

@filp
Copy link
Author

filp commented Jul 17, 2012

@franzliedke: Yes, thus fixing the error with the magic healing power of heuristics.

@franzliedke
Copy link

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