Error Stack Ignore

Overview

This example will show you how to ignore HTML_Progress2 errors but keep them in stack.
Exceptions will be catched and script won't die anymore, so line 92 will be reached !

Screenshot

PB1 html_progress2_errorstack (0)
invalid input, parameter #3 "$min" was expecting "integer", instead got "string"

PB2 html_progress2_errorstack (0)
invalid input, parameter #1 "$min" was expecting "positive", instead got "-1"

PB2 html_progress2_errorstack (1)
invalid input, parameter #1 "$continuous" was expecting "boolean", instead got "string"

still alive !

Demonstration

warning For security reasons live demo is not available.

Dependencies

This example requires mandatory resources :

Explains step by step

Render options on this example are the same than for example ErrorHandling/PEAR_ErrorStack/Custom.
Changes are only on local error handler (lines 24, 44-47); this function returns value of PEAR_ERRORSTACK_IGNORE constant to simply ignore all errors that occurs.

Even if errors were ignored for loggers, they are still available on HTML_Progress2 stack.

See also :

Source Code

  1. <?php
  2. /**
  3. * Simply ignores html_progress2 errors that occurs
  4. * with PEAR_ErrorStack handler.
  5. *
  6. * @version    $Id: errorstackignore.php,v 1.1 2005/06/12 21:03:04 farell Exp $
  7. * @author     Laurent Laville <pear@laurent-laville.org>
  8. * @package    HTML_Progress2
  9. * @subpackage Examples
  10. * @access     public
  11. */
  12. require_once 'HTML/Progress2.php';
  13. require_once 'HTML/Progress2/Error.php';
  14. require_once 'PEAR/ErrorStack.php';
  15.  
  16. class HTML_Progress2_ErrorStack
  17. {
  18.     function HTML_Progress2_ErrorStack()
  19.     {
  20.         $s = &PEAR_ErrorStack::singleton('HTML_Progress2');
  21.         $t = HTML_Progress2_Error::_getErrorMessage();
  22.         $s->setErrorMessageTemplate($t);
  23.         $s->setContextCallback(array(&$this,'getBacktrace'));
  24.         $s->pushCallback(array(&$this,'errorHandler'));
  25.     }
  26.  
  27.     function push($code, $level, $params)
  28.     {
  29.         $s = &PEAR_ErrorStack::singleton('HTML_Progress2');
  30.         return $s->push($code, $level, $params);
  31.     }
  32.  
  33.     function getBacktrace()
  34.     {
  35.         if (function_exists('debug_backtrace')) {
  36.             $backtrace = debug_backtrace();
  37.             $backtrace = $backtrace[count($backtrace)-1];
  38.         } else {
  39.             $backtrace = false;
  40.         }
  41.         return $backtrace;
  42.     }
  43.  
  44.     function errorHandler()
  45.     {
  46.         return PEAR_ERRORSTACK_IGNORE;
  47.     }
  48. }
  49.  
  50. function dump($title, $e)
  51. {
  52.     echo "<h1> $title </h1>";
  53.     print_r($e['message']);
  54.     echo '<br/>';
  55. }
  56.  
  57.  
  58. // Example A. ---------------------------------------------
  59.  
  60. $stack =& new HTML_Progress2_ErrorStack();
  61.  
  62. $prefs = array('error_handler' => array(&$stack, 'push'));
  63.  
  64. // A1. Exception
  65. $pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
  66.  
  67.  
  68. $countErrors = $pb1->hasErrors();
  69. for ($i=0; $i<$countErrors; $i++) {
  70.     $e = $pb1->getError();
  71.     dump('PB1 html_progress2_errorstack ('.$i.')', $e);
  72. }
  73.  
  74.  
  75. // Example B. ---------------------------------------------
  76.  
  77. $pb2 = new HTML_Progress2($prefs);
  78.  
  79. // B1. Error
  80. $pb2->setMinimum(-1);
  81.  
  82. // B2. Exception
  83. $pb2->setIndeterminate('true');
  84.  
  85.  
  86. $countErrors = $pb2->hasErrors();
  87. for ($i=0; $i<$countErrors; $i++) {
  88.     $e = $pb2->getError();
  89.     dump('PB2 html_progress2_errorstack ('.$i.')', $e);
  90. }
  91.  
  92. print 'still alive !'
  93.  
  94. ?>