Error Stack Custom
Overview
This example will show you how to cutomize HTML_Progress2 error handling
with PEAR_ErrorStack.
Exceptions will be catched and script won't die anymore, so line 122 will be reached !
Screenshot
Html_progress2_errorstack exception:
invalid input, parameter #3 "$min" was expecting "integer", instead got "string"
[in [path_to]\errorstackcustom.php on line 99]
Html_progress2_errorstack error:
invalid input, parameter #1 "$min" was expecting "positive", instead got "-1"
File: [path_to]\errorstackcustom.php
Line: 117
Html_progress2_errorstack exception:
invalid input, parameter #1 "$continuous" was expecting "boolean", instead got "string"
File: [path_to]\errorstackcustom.php
Line: 120
still alive !
Demonstration
For security reasons live demo is not available
and i've replaced the real path to file errorstackcustom.php by [path_to].
But in real conditions, you will see it!
Dependencies
This example requires mandatory resources :
Explains step by step
On example A, all errors will be proceed by the user callback
HTML_Progress2_ErrorStack::push (lines 94,96),
class declared on lines 15-86.
Resource (line 12), HTML_Progress2_Error class, is only necessary and used to
get HTML_Progress2 error message templates (line 20).
To avoid a wrong context frame (which should always returns line 31),
a new context callback was declared (lines 22, 34-43).
To catch exception and avoid script to halt (default behaviour), a new local error callback
was declared (lines 25, 76-85).
If you want to restore default behaviour, you've just to set value of
variable $halt_onException (line 89) to true.
Because PEAR_ErrorStack as no logger defined by default, we set/declared our own
(lines 23-24, 45-74).
Its goals are only to print error message on browser screen (lines 70-73).
Option | Value | Default |
error_handler | HTML_Progress2_ErrorStack::push | HTML_Progress2::_errorHandler |
handler | | display = array([2]) |
Default options of display handler (lines 49-50):
display [2] | Value |
lineFormat | <b>%1\$s</b>: %2\$s<br/>[%3\$s]<hr/> |
contextFormat | in <b>%1\$s</b> on line <b>%2\$s</b> |
See also :
On example B, all errors are still proceed by the user callback
HTML_Progress2_ErrorStack::push (lines 94,110).
As we also want to beautify display render, we changed the default display driver options
(lines 104-108) activated on line 111.
Option | Value | Default |
error_handler | HTML_Progress2_ErrorStack::push | HTML_Progress2::_errorHandler |
handler | display = array([1]) | display = array([2]) |
Custom options of display handler:
display [1] | Value |
lineFormat | <b>%1\$s</b>: %2\$s<br/>%3\$s<hr/> |
contextFormat |
<b>File:</b> %1\$s <br/>
<b>Line:</b> %2\$s |
Source Code
<?php
/**
* Customize error renderer with PEAR_ErrorStack.
*
* @version $Id: errorstackcustom.php,v 1.1 2005/06/12 21:03:04 farell Exp $
* @author Laurent Laville <pear@laurent-laville.org>
* @package HTML_Progress2
* @subpackage Examples
* @access public
*/
require_once 'HTML/Progress2.php';
require_once 'HTML/Progress2/Error.php';
require_once 'PEAR/ErrorStack.php';
class HTML_Progress2_ErrorStack
{
function HTML_Progress2_ErrorStack()
{
$s = &PEAR_ErrorStack::singleton('HTML_Progress2');
$t = HTML_Progress2_Error::_getErrorMessage();
$s->setErrorMessageTemplate($t);
$s->setContextCallback(array(&$this,'getBacktrace'));
$logger = array(&$this,'log');
$s->setLogger($logger);
$s->pushCallback(array(&$this,'errorHandler'));
}
function push($code, $level, $params)
{
$s = &PEAR_ErrorStack::singleton('HTML_Progress2');
return $s->push($code, $level, $params);
}
function getBacktrace()
{
if (function_exists('debug_backtrace')) {
$backtrace = debug_backtrace();
$backtrace = $backtrace[count($backtrace)-1];
} else {
$backtrace = false;
}
return $backtrace;
}
function log($err)
{
global $prefs;
$lineFormat = '<b>%1$s:</b> %2$s<br/>[%3$s]<hr/>'."<br/>\n";
$contextFormat = 'in <b>%1$s</b> on line <b>%2$s</b>';
if (isset($prefs['handler']['display']['lineFormat'])) {
$lineFormat = $prefs['handler']['display']['lineFormat'];
}
if (isset($prefs['handler']['display']['contextFormat'])) {
$contextFormat = $prefs['handler']['display']['contextFormat'];
}
$context = $err['context'];
if ($context) {
$file = $context['file'];
$line = $context['line'];
$contextExec = sprintf($contextFormat, $file, $line);
} else {
$contextExec = '';
}
printf($lineFormat,
ucfirst(get_class($this)) . ' ' . $err['level'],
$err['message'],
$contextExec);
}
function errorHandler($err)
{
global $halt_onException;
if ($halt_onException) {
if ($err['level'] == 'exception') {
return PEAR_ERRORSTACK_DIE;
}
}
}
}
// set it to on if you want to halt script on any exception
$halt_onException = false;
// Example A. ---------------------------------------------
$stack =& new HTML_Progress2_ErrorStack();
$prefs = array('error_handler' => array(&$stack, 'push'));
// A1. Exception
$pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
// Example B. ---------------------------------------------
$displayConfig = array(
'lineFormat' => '<b>%1$s</b>: %2$s<br/>%3$s<hr/>',
'contextFormat' => '<b>File:</b> %1$s <br/>'
. '<b>Line:</b> %2$s '
);
$prefs = array(
'error_handler' => array(&$stack, 'push'),
'handler' => array('display' => $displayConfig)
);
$pb2 = new HTML_Progress2($prefs);
// B1. Error
$pb2->setMinimum(-1);
// B2. Exception
$pb2->setIndeterminate('true');
print 'still alive !';
?>