PEAR Progress2 logo

HTML_Progress2 : The Definitive Guide

Controlling error generation

There are many scenarios in which fine-grained control over error raising is absolutely necessary.

The first level to control error generation is the php.ini directives display_errors and log_errors. When these directives are set to TRUE, then browser and file outputs are effective.

[Tip] Tip
If you want to ignore all errors raised (no display, no logs) and avoid to include PEAR core class, then you should have something like :
  1. <?php
  2. require_once 'HTML/Progress2.php';
  3.  
  4. function myErrorHandler()
  5. {
  6.     return null;
  7. }
  8.  
  9. $errorConf = array('error_handler' => 'myErrorHandler');
  10. $meter = new HTML_Progress2($errorConf);
  11. // ...
  12. ?>
[Caution] Caution
The previous example will ignore display and logging activity, but NEVER ignore internal stack error. In other words, you'll keep always minimum information in the progress stack error. These informations are :
  1. <?php
  2. array('code' => $code, 'level' => $level, 'params' => $params);
  3. /*
  4.       $code  : API error code (HTML_PROGRESS2_ERROR_* constant value)
  5.       $level : API error level (warning, error, exception)
  6.       $params: API context execution parameters hash (function argument: name, type, value)
  7.  */
  8. ?>
So, these lines are always true (in error condition):
  1. <?php
  2. $meter = new HTML_Progress2();
  3. // ...
  4. if ($meter->hasErrors() > 0) {
  5.     // do something on error ...
  6. }
  7. ?>

It's up to you to choose your programming way. Just keep in mind this warning, in case of code evolution (error handler configuration). Behavior could change, and you won't keep backward compatibility.

With push_callback option, you can decides to stop script execution (as done with exceptions by default: returns PEAR_ERROR_DIE constant), or continue without filtering (returns NULL).

If you want to write your own callback function for the push_callback option, this one should have two arguments: first one will get the error code, and second will get error level. These are all the necessary informations to do a filtering. Example that follow show how to be aware that a deprecated function is used in a script.

  1. <?php
  2. require_once 'HTML/Progress2.php';
  3.  
  4. function myErrorFilter($code, $level)
  5. {
  6.     if ($code === HTML_PROGRESS2_ERROR_DEPRECATED) {
  7.         error_log('script: '.__FILE__.' still used a deprecated function', 1, 'admin@yoursite.com');
  8.     }
  9.     return null;
  10. }
  11.  
  12. $errorConf = array('push_callback' => 'myErrorFilter');
  13. $meter = new HTML_Progress2($errorConf);
  14. // ...
  15. ?>
HTML_Progress2 : The Definitive Guide v 2.4.0 : April 20, 2007