PEAR logo

HTML_CSS : 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/CSS.php';
  3.  
  4. function myErrorHandler()
  5. {
  6.     return null;
  7. }
  8.  
  9. $errorConf = array('error_handler' => 'myErrorHandler');
  10. $css = new HTML_CSS(null, $errorConf);
  11. // ...
  12. ?>
[Note] Note for users of HTML_CSS 1.4.0 or greater

You may want to decide to print (yes/no), log (yes/no) error messages with a full free user function local to HTML_CSS

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. function myErrorAction($css_error)
  5. {
  6.     // do what you want: print and/or log $css_error instance of HTML_CSS_Error object
  7. }
  8.  
  9. $errorConf = array('error_callback' => 'myErrorAction');
  10. $css = new HTML_CSS(null, $errorConf);
  11. // ...
  12. ?>

rather than using global PEAR error handler (PEAR::setErrorHandling, PEAR::pushErrorHandling, PEAR::popErrorHandling).

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. function myErrorAction($css_error)
  5. {
  6.     // do what you want: print and/or log $css_error instance of HTML_CSS_Error object
  7. }
  8.  
  9. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'myErrorAction');
  10.  
  11. $css = new HTML_CSS();
  12. // ...
  13. ?>

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 script use wrong argument data type.

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. function myErrorFilter($code, $level)
  5. {
  6.     if ($code === HTML_CSS_ERROR_INVALID_INPUT) {
  7.         error_log('script: '.__FILE__.' used wrong argument data type', 1, 'admin@yoursite.com');
  8.     }
  9.     return null;
  10. }
  11.  
  12. $errorConf = array('push_callback' => 'myErrorFilter');
  13. $css = new HTML_CSS(null, $errorConf);
  14. // ...
  15. ?>
HTML_CSS : The Definitive Guide v 1.5.0 : January 15, 2008