I am about to start implementing error handling into my framework and looking for some advice on how to build it.
Firstly let me explain how my framework is currently built up:
Im separating the framework startup from the application startup, so any errors caused within the application startup should be handled specifically by a class dedicated to that.
My idea is to have a class called Core_Error_exception
witch will set the error reporting to E_ALL
as my framework will be strict on errors for PHP 5.3, then as my as my application load I will run a shutdown function function within that class to restore all default values changed.
What im looking to do is capture all errors that are E_*_NOTICE
and not E_*_ERROR
and then before the application starts I tell the class to stop capturing errors as the Application_Error_Exception
will be watching out for errors.
So i will need a way to track all errors including exceptions and triggers and then before the application initializes show a framework debug page.
The kind of class I was looking for is like so:
class Core_Error_Exception
{
var $previus_error_level,$captured_contents;
private $stack_trace = array();
public function Core_Error_Exception()
{
$this->previus_error_level = error_reporting(-1);
set_error_handler(array($this,'_collect_error'));
set_exception_handler(array($this,'_collect_error'));
ob_start(array($this,'_capture'));
}
public function _collect_error($errno, $errstr, $errfile, $errline, $context)
{
$this->stack_trace[] = array(
array('name' => 'Error ID:', 'value' => $errno),
array('name' => 'Error String:','value' => $errstr),
array('name' => 'Error File:', 'value' => $errfile),
array('name' => 'Error Line:', 'value' => $errline),
array('name' => 'Context PRE:', 'value' => $context)
);
var_dump($this->stack_trace);
}
/*
* _capture is used to capture pre_bufferd content.
*/
public function _capture($content,$bitfeild)
{
if($bitfeild & PHP_OUTPUT_HANDLER_START)
{
$this->captured_contents = $content;
}
if($bitfeild & PHP_OUTPUT_HANDLER_CONT)
{
$this->captured_contents .= $content;
}
if($bitfeild & PHP_OUTPUT_HANDLER_END)
{
$this->captured_contents .= $content;
}
return false;
}
}
So what im looking to do is to be able to construct this class in a fall-proof way so that any notice errors that may have been triggered will be placed into an array, if an E_ERROR notice is called then this automatically run the shut-down at that point, to prevent more errors being caused.
I will be using a small html template handler where I can pass into that context sets of errors, so Notice errors and a single E_*_ERROR
if applicable.
Whats the best way to build this class as in the past I have had some difficulty in doing Error Tracking / Reporting.
Updated: with current class
If errors are triggered such as trigger_error('test',XXX); I want to be able to track all errors until the application launches or an E_USER_ERROR has been triggered.
Sometimes I struggle to fully grasp PHP's error system, and so on as sometimes i get confused in how to build it so its fall-proof.