lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PHP: Error Handling
An old error handling function that I am retiring. I have a more advanced SESSION handler that can track errors between page redirects that I use now.

/* Fx E_handler
******************************/
function E_handler($e_num, $e_msg, $filename, $linenum, $vars) {

// *** DATA

// global
global $E_TYPE, $_E_version, $E_local_mail, $E_local_log;

// internal variables
$_E_info = '';
$_bug_trace = '';

// timestamp for the error entry
$_E_stamp = date("Y-m-d H:i:s (T)");

// output styling

# console
$_console_style = 'position:relative;'
. 'padding:0; margin:2em 0 1em; text-align:left;'
. 'background-color:#683431; border:thin solid red; border-width:1px 0 10px; '
. 'color:#00ff00; font-family:sans-serif; font-size:11px;';

# titlebar
$_titlebar_style = 'position:relative; background:red; padding:.2em; '
. 'border:thin solid white; border-width:1px 0 0;';

# h2
$h2_style = 'color:white; font-family:sans-serif; font-size:12px; margin:.1em;';

# screen
$_screen_style = 'position:relative;'
. 'background:none; padding:6px 0 0;'
. 'border:thin solid red; border-width:0 4px; '
. 'text-align:left; font-family:Courier New,monospace; font-size:11px; font-weight:bold;';

# h3
$h3_style = 'padding:.2em .5em; margin:10px 0 0; '
. 'color:white; font-family:sans-serif; font-size:11px; '
. 'background:red;';

# trace box
$_trace_style = 'position:relative; height:125; overflow:auto; padding:0 .5em;'
. 'color:#dbe3e8; background:#4f6a7b; '
. 'text-align:left; font-size:10px;';


// E_STRICT: PHP5 only
if ( defined('E_STRICT') ) { $errortype[E_STRICT] = "Runtime Notice"; }


// *** MANIPULATE

// suppress array index warnings (http://www.php.net/manual/en/function.error-reporting.php)
if ( $e_num == E_NOTICE && substr($e_msg, 0, 17) == "Undefined index: " ) { return; }

// error_reporting off -> suppress
if ( error_reporting() == 0 ) { return; }

// backtrace
$BUG = debug_backtrace();
$_file = $BUG[1]['file'];
$_line = $BUG[1]['line'];
# $_bug_trace = print_r($BUG, TRUE);

// filter ehandler
$_steps = count($BUG) + 1;
foreach ( $BUG as $_fx_step ) {
$_steps--;
if ( $_fx_step['function'] == 'E_handler' ) {
continue;
}
$_bug_trace .= '<div style="border-bottom:thin solid #dbe3e8; padding:0 .5em .5em;">'
. "<h3 style=\"margin:0; text-decoration:underline;\">Function Step #{$_steps} -> {$_fx_step['function']}()</h3>"
. htmlspecialchars(print_r($_fx_step, TRUE)) . "</div>\n";
}


// html encode
# $_bug_trace = htmlspecialchars($_bug_trace);

// build error message
$_E_info = <<<EINFO

<errortype><b>$E_TYPE[$e_num]</b></errortype> ->
<errormsg style="font-style:italic; font-family:Times New Roman,serif;">$e_msg</errormsg>
(<errornum>Error #$e_num</errornum>)<br />
File: <scriptname>$filename</scriptname><br />
Line: <scriptlinenum>$linenum</scriptlinenum><br />
<datetime>$_E_stamp</datetime><br />

EINFO;

// construct trace box (display only in local or alpha versions)
$_trace_box = <<<TRACE

<h3 style="$h3_style">&raquo; debugger trace</h3>
<div id="trace_box" style="$_trace_style">
<pre>$_bug_trace</pre>
</div>

TRACE;

if ( VERSION != 'local' && VERSION != 'alpha' ) {
$_trace_box = '<!-- suppressed -->';
}

// generate output
$output = <<<ERR


<!-- ERROR CONSOLE -->
<div id="eh_console" class="error_report" style="$_console_style">

<!-- TITLEBAR -->
<div id="eh_titlebar" style="$_titlebar_style">
<h2 style="$h2_style">Error &raquo; {$E_TYPE[$e_num]}</h2>
</div>

<!-- REPORT SCREEN -->
<div id="eh_screen" style="$_screen_style">
<div id="e_info" style="padding-left:1em;">$_E_info</div>

<!-- TRACE BOX -->
$_trace_box

</div>
<!-- end SCREEN -->

</div>
<!-- end ERROR -->


ERR;

// vary output according to version

# local -> echo[, log, mail]
if ( VERSION == 'local' ) {
echo $output;
if ( $E_local_mail ) { E_mail(); }
if ( $E_local_log ) { E_log(); }
if ($e_num == E_USER_ERROR) {
die("\n<div id=\"fatal_error\"><b>FATAL ERROR at $_file: $_line</b><br />\n In the event of live application, you would be redirected to an error page</div>");
}
}

# alpha -> echo, log
elseif ( VERSION == 'alpha' ) {
echo $output;
E_log();
if ($e_num == E_USER_ERROR) {
die("\n<div id=\"fatal_error\"><b>FATAL ERROR at $_file: $_line</b><br />\n In the event of live application, you would be redirected to an error page</div>");
}
}

# beta -> echo, log, mail
elseif ( VERSION == 'beta' ) {
echo $output;
E_log();
E_mail();
if ($e_num == E_USER_ERROR) {
die("\n<div id=\"fatal_error\"><b>FATAL ERROR at $_file: $_line</b><br />\n In the event of live application, you would be redirected to an error page</div>");
}
}
# live -> log, mail fatal, redirect
else {
E_log($output, 3, E_LOG);
if ( ($e_num == E_USER_ERROR) || ($e_num == E_USER_WARNING) ) {
E_mail(SP_CONTACT, 'Critical User Error', $output);
E_redirect();
}
}


/* alpha version */
if ( $ep_version == 'alpha' ) {

}
/* beta/test_live version */
elseif ( $ep_version == 'beta' || $ep_version == 'test_live' ) {
$vartrace = "<div style=\"$trace_style\"><pre><h2>DEBUG TRACE</h2>$bug_trace</pre></div>";
echo "\n\n<!-- ERROR -->\n<div id=\"errorentry\"".$err_style.">\n".$err.$vartrace."</div>\n<!-- end ERROR -->\n\n";
}


} /* end Fx */
/*****************************/