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

warning 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

  1. <?php
  2. /**
  3. * Customize error renderer with PEAR_ErrorStack.
  4. *
  5. * @version    $Id: errorstackcustom.php,v 1.1 2005/06/12 21:03:04 farell Exp $
  6. * @author     Laurent Laville <pear@laurent-laville.org>
  7. * @package    HTML_Progress2
  8. * @subpackage Examples
  9. * @access     public
  10. */
  11. require_once 'HTML/Progress2.php';
  12. require_once 'HTML/Progress2/Error.php';
  13. require_once 'PEAR/ErrorStack.php';
  14.  
  15. class HTML_Progress2_ErrorStack
  16. {
  17.     function HTML_Progress2_ErrorStack()
  18.     {
  19.         $s = &PEAR_ErrorStack::singleton('HTML_Progress2');
  20.         $t = HTML_Progress2_Error::_getErrorMessage();
  21.         $s->setErrorMessageTemplate($t);
  22.         $s->setContextCallback(array(&$this,'getBacktrace'));
  23.         $logger = array(&$this,'log');
  24.         $s->setLogger($logger);
  25.         $s->pushCallback(array(&$this,'errorHandler'));
  26.     }
  27.  
  28.     function push($code, $level, $params)
  29.     {
  30.         $s = &PEAR_ErrorStack::singleton('HTML_Progress2');
  31.         return $s->push($code, $level, $params);
  32.     }
  33.  
  34.     function getBacktrace()
  35.     {
  36.         if (function_exists('debug_backtrace')) {
  37.             $backtrace = debug_backtrace();
  38.             $backtrace = $backtrace[count($backtrace)-1];
  39.         } else {
  40.             $backtrace = false;
  41.         }
  42.         return $backtrace;
  43.     }
  44.  
  45.     function log($err)
  46.     {
  47.         global $prefs;
  48.        
  49.         $lineFormat = '<b>%1$s:</b> %2$s<br/>[%3$s]<hr/>'."<br/>\n";
  50.         $contextFormat = 'in <b>%1$s</b> on line <b>%2$s</b>';
  51.  
  52.         if (isset($prefs['handler']['display']['lineFormat'])) {
  53.             $lineFormat = $prefs['handler']['display']['lineFormat'];
  54.         }
  55.         if (isset($prefs['handler']['display']['contextFormat'])) {
  56.             $contextFormat = $prefs['handler']['display']['contextFormat'];
  57.         }
  58.  
  59.         $context = $err['context'];
  60.            
  61.         if ($context) {
  62.             $file  = $context['file'];
  63.             $line  = $context['line'];
  64.                
  65.             $contextExec = sprintf($contextFormat, $file, $line);
  66.         } else {
  67.             $contextExec = '';
  68.         }
  69.    
  70.         printf($lineFormat,
  71.                ucfirst(get_class($this)) . ' ' . $err['level'],
  72.                $err['message'],
  73.                $contextExec);
  74.     }
  75.  
  76.     function errorHandler($err)
  77.     {
  78.         global $halt_onException;
  79.        
  80.         if ($halt_onException) {
  81.             if ($err['level'] == 'exception') {
  82.                 return PEAR_ERRORSTACK_DIE;
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. // set it to on if you want to halt script on any exception
  89. $halt_onException = false;
  90.  
  91.  
  92. // Example A. ---------------------------------------------
  93.  
  94. $stack =& new HTML_Progress2_ErrorStack();
  95.  
  96. $prefs = array('error_handler' => array(&$stack, 'push'));
  97.  
  98. // A1. Exception
  99. $pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
  100.  
  101.  
  102. // Example B. ---------------------------------------------
  103.  
  104. $displayConfig = array(
  105.     'lineFormat' => '<b>%1$s</b>: %2$s<br/>%3$s<hr/>',
  106.     'contextFormat' =>   '<b>File:</b> %1$s <br/>'
  107.                        . '<b>Line:</b> %2$s '
  108. );
  109. $prefs = array(
  110.     'error_handler' => array(&$stack, 'push'),
  111.     'handler' => array('display' => $displayConfig)
  112. );
  113.  
  114. $pb2 = new HTML_Progress2($prefs);
  115.  
  116. // B1. Error
  117. $pb2->setMinimum(-1);
  118.  
  119. // B2. Exception
  120. $pb2->setIndeterminate('true');
  121.  
  122. print 'still alive !'
  123.  
  124. ?>