Custom

Overview

This example will show you how to cutomize HTML_Progress2 error handling with standard PEAR_Error object.

Exceptions will be catched and script won't die anymore, so line 82 will be reached !

Screenshot

error_message_prefix =
mode = 16
level = 1024
code = -100
message = invalid input, parameter #3 "$min" was expecting "integer", instead got "string"


Error: invalid input, parameter #1 "$min" was expecting "positive", instead got "-1"
File: [path_to]\errorcustom.php
Line: 77
Function: html_progress2->setminimum


Exception: invalid input, parameter #1 "$continuous" was expecting "boolean", instead got "string"
File: [path_to]\errorcustom.php
Line: 80
Function: html_progress2->setindeterminate


still alive !

Demonstration

warning For security reasons live demo is not available and i've replaced the real path to file errorcustom.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 myErrorCallback (line 53), function declared on lines 23-31.

This function print only some PEAR_Error object properties.

To catch exception and avoid script to halt (default behaviour), the push_callback option (line 55) set function my1ErrorHandler declared on lines 33-36. This function will returns NULL value, that means PEAR_Error object follows the PEAR::setErrorHandling directive.

Option Value Default
push_callback my1ErrorHandler HTML_Progress2_Error::_handleError
See also :

On example B, all errors will be proceed by the default error callback (HTML_Progress2::_errorHandler) that always raised a PEAR_Error object with mode returns by the push_callback user function (a valid PEAR_ERROR_* constant).

In our case, the push_callback option set function my2ErrorHandler (line 70), declared on lines 38-41. This function will returns PEAR_ERROR_CALLBACK to force HTML_Progress2 error handler to use the HTML_Progress2_Error::log method.
This method will send error message on browser and/or on log file, depending on the php.ini directives value display_errors, log_errors.

To be sure to have something on display and in the default 'html_progress_error.log' file, php.ini directives were set to On (lines 47-48).

As we also want to beautify display render, we changed the default display driver options (lines 63-68) activated on line 71.

Option Value Default
push_callback my2ErrorHandler HTML_Progress2_Error::_handleError
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 <br />
<b>Function:</b> %3\$s

Default options of display handler:

display [2] Value
eol <br/>\\n
lineFormat <b>%1\$s</b>: %2\$s %3\$s
contextFormat in <b>%3\$s</b> (file <b>%1\$s</b> on line <b>%2\$s</b>)
See also :

Source Code

  1. <?php
  2. /**
  3. * Customize error renderer with default PEAR_Error object.
  4. *
  5. * @version    $Id: errorcustom.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 'PEAR.php';
  13.  
  14. function dump($title, $e)
  15. {
  16.     echo "<h1> $title </h1>";
  17.     echo '<pre>';
  18.     var_dump($e);
  19.     echo '</pre>';
  20.     echo '<hr/>';
  21. }
  22.  
  23. function myErrorCallback($pb_error)
  24. {
  25.     $keys = array('error_message_prefix', 'mode', 'level', 'code', 'message');
  26.    
  27.     foreach ($keys as $i => $k) {
  28.         printf("%s = %s <br/>\n", $k, $pb_error->$k);
  29.     }
  30.     echo '<hr/>';
  31. }
  32.  
  33. function my1ErrorHandler()
  34. {
  35.     return null;
  36. }
  37.  
  38. function my2ErrorHandler()
  39. {
  40.     return PEAR_ERROR_CALLBACK;
  41. }
  42.  
  43. /*
  44. * be sure that we will print and log error details.
  45. * @see HTML_Progress2_Error::log()
  46. */
  47. ini_set('display_errors',1);
  48. ini_set('log_errors',1);
  49.  
  50.  
  51. // Example A. ---------------------------------------------
  52.  
  53. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'myErrorCallback');
  54.  
  55. $prefs = array('push_callback' => 'my1ErrorHandler');
  56.  
  57. // A1. Exception
  58. $pb1 = new HTML_Progress2($prefs, HTML_PROGRESS2_BAR_VERTICAL, '0', 130);
  59.  
  60.  
  61. // Example B. ---------------------------------------------
  62.  
  63. $displayConfig = array(
  64.     'lineFormat' => '<b>%1$s</b>: %2$s<br/>%3$s<hr/>',
  65.     'contextFormat' =>   '<b>File:</b> %1$s <br />'
  66.                        . '<b>Line:</b> %2$s <br />'
  67.                        . '<b>Function:</b> %3$s '
  68. );
  69. $prefs = array(
  70.     'push_callback' => 'my2ErrorHandler',
  71.     'handler' => array('display' => $displayConfig)
  72. );
  73.  
  74. $pb2 = new HTML_Progress2($prefs);
  75.  
  76. // B1. Error
  77. $pb2->setMinimum(-1);
  78.  
  79. // B2. Exception
  80. $pb2->setIndeterminate('true');
  81.  
  82. print 'still alive !'
  83.  
  84. ?>