HTML_Progress
[ class tree: HTML_Progress ] [ index: HTML_Progress ] [ all elements ]
Prev Next
Advanced Error Handling
introduction to a new error-handling system

Authors

Laurent Laville
Gregory Beaver

Special thanks to Gregory Beaver, for his works on PEAR_ErrorStack (part of PEAR core >= 1.3.1) and its manual, source of inspiration for the new HTML_Progress error handling system.


Table of Contents

Introduction

Why write a new error-handling routine when PEAR_Error already exists ? There are several problems with PEAR_Error. Although an error message is present in an error class, processing this error message automatically is excessively difficult for computers. In addition, the error message cannot easily be translated once it has been placed into the PEAR_Error object. There is also no standard facility for storing error-related data in the error class. On top of error message-related issues, there is no way to automatically determine which package a PEAR_Error object comes from, or the severity of an error. Fatal errors look exactly the same as non-fatal errors.

HTML_Progress implements error raising and handling using a stack pattern like PEAR_ErrorStack. So why don't just use PEAR_ErrorStack rather than rewrite the same concept. HTML_Progress is not a copy of features of PEAR_ErrorStack, even it allows to plug-in any error-handler routine you might want to have.

Features of HTML_Progress error handling system include :

  • Error levels (notice/warning/error/exception)
  • Error context data is saved separate from error message
  • Dynamic error message generation
  • Sophisticated callbacks are available for error message generation, error context generation, and error handling functionality
  • Use your own error-handler

Default error handling system of HTML_Progress allow to :

  • Display or not any error message with control of PHP INI display_errors value
  • Display error message as the default PHP error handler (Error level and File, Line context in bold face)

Basic example

Now with this example, we will demonstrate the basic use and result of HTML_Progress error handler.

  1. <?php
  2. require_once 'HTML/Progress.php';
  3. $bar = new HTML_Progress();
  4.  
  5. $e = $bar->setDM('dm_class_model');
  6.  
  7. if (is_object($e)) {
  8. if (is_a($e,'PEAR_Error')) {
  9. die('<h1>Catch PEAR_Error</h1>'. $e->toString());
  10. }
  11. }
  12. ?>

We will get the message below only if PHP INI display_errors is enable ('on' | '1' ).

Error: invalid input, parameter #1 "$model" was expecting "dm_class_model class defined", instead got "class does not exists"  
in html_progress->setdm 
(file d:\php\pear\html_progress\tutorials\html_progress\examples\eh_basic_display.php at line 6)

For basic use, this is all you have to know to use HTML_Progress package to raise PEAR_Error object without its disadvantages.


Advanced features

Error Context Display

In some cases, you may want to customize error generation. For instance, for many exceptions, it is useful to include file, line number, and class/function context information in order to trace an error. A default option is available (context_callback) which will be sufficient for most cases.

Let's have a look on the default context callback routine : HTML_Progress::_getBacktrace

  1. <?php
  2. function _getBacktrace()
  3. {
  4. if (function_exists('debug_backtrace')) {
  5. $backtrace = debug_backtrace(); // PHP 4.3+
  6. $backtrace = $backtrace[count($backtrace)-1];
  7. } else {
  8. $backtrace = false; // PHP 4.1.x, 4.2.x (no context info available)
  9. }
  10. return $backtrace;
  11. }
  12. ?>

This function generates a PHP backtrace and returns this information as an associative array. See http://www.php.net/debug_backtrace for details.

If you wish context information to be in the error message, the error handler callback (option error_handler) should add the information in a human-readable format to the error message.

Let's have a look on part of the default error callback routine : HTML_Progress::_errorHandler

  1. <?php
  2. function _errorHandler($err)
  3. {
  4. include_once 'PEAR.php';
  5. $e = PEAR::raiseError($err['message'], $err['code'], PEAR_ERROR_RETURN, E_USER_ERROR,
  6. $err['context']);
  7.  
  8. if (isset($err['context'])) {
  9. $file = $err['context']['file'];
  10. $line = $err['context']['line'];
  11. $func = $err['context']['class'];
  12. $func .= $err['context']['type'];
  13. $func .= $err['context']['function'];
  14. }
  15.  
  16. $display_errors = ini_get('display_errors');
  17. $log_errors = ini_get('log_errors');
  18.  
  19. $display = $GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS']['display'];
  20. if ($display_errors) {
  21. $lineFormat = $display['conf']['lineFormat'];
  22. $contextFormat = $display['conf']['contextFormat'];
  23.  
  24. $context = sprintf($contextFormat, $file, $line, $func);
  25. printf($lineFormat."<br />\n", ucfirst($err['level']), $err['message'], $context);
  26. }
  27. if ($log_errors) {
  28. // more code here ... but hidden
  29. }
  30. return $e;
  31. }
  32. ?>

Context data are merged into error message with help of lineFormat and contextFormat configuration options (see Default Display Handler)


Custom Error Message Generation

Let's have a look on the default message callback routine : HTML_Progress::_msgCallback

  1. <?php
  2. function _msgCallback($err)
  3. {
  4. $messages = HTML_Progress::_getErrorMessage();
  5. $mainmsg = $messages[$err['code']];
  6. if (count($err['params'])) {
  7. foreach ($err['params'] as $name => $val) {
  8. if (is_array($val)) {
  9. $val = implode(', ', $val);
  10. }
  11. $mainmsg = str_replace('%' . $name . '%', $val, $mainmsg);
  12. }}
  13. return $mainmsg;
  14. }
  15.  
  16. function _getErrorMessage()
  17. {
  18. $messages = array(
  19. HTML_PROGRESS_ERROR_INVALID_INPUT =>
  20. 'invalid input, parameter #%paramnum% '
  21. . '"%var%" was expecting '
  22. . '"%expected%", instead got "%was%"',
  23. HTML_PROGRESS_ERROR_INVALID_CALLBACK =>
  24. 'invalid callback, parameter #%paramnum% '
  25. . '"%var%" expecting %element%,'
  26. . ' instead got "%was%" does not exists',
  27. HTML_PROGRESS_DEPRECATED =>
  28. 'method is deprecated '
  29. . 'use %newmethod% instead of %oldmethod%'
  30.  
  31. );
  32. return $messages;
  33. }
  34. ?>

HTML_Progress::_getErrorMessage set and return an array mapping error codes to error message templates.

Substitution is done (line 12) using http://www.php.net/str_replace. Basically, if a variable name is enclosed in percent sign (%), it will be replaced with the value passed in the associative array (line 8).


Controlling error generation

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

We can influence the error management through the use of five constants:

  • HTML_PROGRESS_ERRORSTACK_PUSHANDLOG informs the stack to push the error onto the error stack, and also to log the error.
  • HTML_PROGRESS_ERRORSTACK_PUSH informs the stack to push the error onto the error stack, but not to log the error.
  • HTML_PROGRESS_ERRORSTACK_LOG informs the stack not to push the error onto the error stack, but only to log the error.
  • HTML_PROGRESS_ERRORSTACK_IGNORE informs the stack to ignore the error, as if it never occured. The error will be neither logged, nor push on the stack. However, a PEAR_Error object will be returned from HTML_Progress::raiseError.
  • HTML_PROGRESS_ERRORSTACK_LOGANDDIE informs the stack not to push the error onto the error stack, but only to log the error and stop the script.

For example, in HTML_Progress Unit Tests: we don't want that processes stop when an exception is raised.

Let's have a look on the script HTML_Progress_TestCase_setIndeterminate.php into the tests directory of this package:

  1. <?php
  2.  
  3. class HTML_Progress_TestCase_setIndeterminate extends PHPUnit_TestCase
  4. {
  5. /**
  6. * HTML_Progress instance
  7. *
  8. * @var object
  9. */
  10. var $progress;
  11.  
  12. function HTML_Progress_TestCase_setIndeterminate($name)
  13. {
  14. $this->PHPUnit_TestCase($name);
  15. }
  16.  
  17. function setUp()
  18. {
  19. error_reporting(E_ALL & ~E_NOTICE);
  20.  
  21. $logger['push_callback'] = array(&$this, '_pushCallback'); // don't die when an exception is thrown
  22. $this->progress = new HTML_Progress($logger);
  23. }
  24.  
  25. function tearDown()
  26. {
  27. unset($this->progress);
  28. }
  29.  
  30. function _stripWhitespace($str)
  31. {
  32. return preg_replace('/\\s+/', '', $str);
  33. }
  34.  
  35. function _methodExists($name)
  36. {
  37. if (substr(PHP_VERSION,0,1) < '5') {
  38. $n = strtolower($name);
  39. } else {
  40. $n = $name;
  41. }
  42. if (in_array($n, get_class_methods($this->progress))) {
  43. return true;
  44. }
  45. $this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->progress));
  46. return false;
  47. }
  48.  
  49. function _pushCallback($err)
  50. {
  51. // don't die if the error is an exception (as default callback)
  52. return HTML_PROGRESS_ERRORSTACK_PUSH;
  53. }
  54.  
  55. function _getResult()
  56. {
  57. if ($this->progress->hasErrors()) {
  58. $err = $this->progress->getError();
  59. $this->assertTrue(false, $err['message']);
  60. } else {
  61. $this->assertTrue(true);
  62. }
  63. }
  64.  
  65. /**
  66. * TestCases for method setIndeterminate.
  67. *
  68. */
  69. function test_setIndeterminate_fail_no_boolean()
  70. {
  71. if (!$this->_methodExists('setIndeterminate')) {
  72. return;
  73. }
  74. $this->progress->setIndeterminate('');
  75. $this->_getResult();
  76. }
  77.  
  78. function test_setIndeterminate()
  79. {
  80. if (!$this->_methodExists('setIndeterminate')) {
  81. return;
  82. }
  83. $this->progress->setIndeterminate(true);
  84. $this->_getResult();
  85. }
  86. }
  87. ?>

Lines 21 and 22, replace the default error handling (push_callback) by our own function (method _pushCallback of the class HTML_Progress_TestCase_setIndeterminate).

Into our new push callback (lines 49 to 53) we informs the stack to always push error, and never die for an exception (default behavior).

Let's have a look on the default push callback routine : HTML_Progress::_handleError

  1. <?php
  2. function _handleError($err)
  3. {
  4. if ($err['level'] == 'exception') {
  5. return HTML_PROGRESS_ERRORSTACK_LOGANDDIE;
  6. }}
  7. ?>

On default behavior, each time an exception is raised, HTML_Progress log the error and halt PHP script execution.


Controlling Error Logging

The next level of control over error output is to selectively log destination. In the default behavior error_handler callback, that is HTML_Progress::_errorHandler, will :

  • return a PEAR_Error object (E_USER_ERROR level, with error code, message, and context as userinfo)
  • log error to browser screen (if allowed by PHP INI display_errors value)
  • log error following rules of http://www.php.net/manual/en/function.error-log.php (if allowed by PHP INI log_errors value)

Default Display Handler

The default display handler has one parameter and two configuration options :

Parameter Default value
conf see array below

Option Default value
lineFormat '<b>%1$s</b>: %2$s %3$s'
contextFormat ' in <b>%3$s</b> (file <b>%1$s</b> at line <b>%2$s</b>)'

with lineFormat parameters:

  • 1$ = error level
  • 2$ = error message
  • 3$ = context line formatted

with contextFormat parameters:

  • $1 = filename
  • $2 = line in file
  • $3 = class call-type function
See http://www.php.net/manual/en/function.debug-backtrace.php for call-type details.


Default Error_Log Handler

The default error_log handler has three parameters and five configuration options :

Parameter Default value
name
ident
conf see array below

with name parameter:

  • HTML_PROGRESS_LOG_TYPE_SYSTEM message is sent to PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error_log configuration directive (PHP INI) is set to.
  • HTML_PROGRESS_LOG_TYPE_MAIL message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used. This message type uses the same internal function as mail() does.
  • HTML_PROGRESS_LOG_TYPE_FILE message is appended to the file destination.

Option Default value
destination
extra_headers
lineFormat '%1$s %2$s [%3$s] %4$s %5$s'
timeFormat '%b %d %H:%M:%S'
contextFormat ' in %3$s (file %1$s at line %2$s)'

with lineFormat parameter:

  • 1$ = error time from time line formatted
  • 2$ = error handler ident
  • 3$ = error level
  • 4$ = error message
  • 5$ = context line formatted

with timeFormat parameter:

with contextFormat parameter:

  • $1 = filename
  • $2 = line in file
  • $3 = class call-type function
See http://www.php.net/manual/en/function.debug-backtrace.php for call-type details.


Ultimate control: Custom Error Handlers

For most of users, the basic and default HTML_Progress error handling system will be enough. But if you want more efficiency, you should set a custom error handler. Let's take an example and follow it to see how to do :

  1. <?php
  2. require_once 'HTML/Progress.php';
  3.  
  4. function _pushCallback($err)
  5. {
  6. // now don't die if the error is an exception, it will be ignored
  7. if ($err['level'] == 'exception') {
  8. return HTML_PROGRESS_ERRORSTACK_IGNORE;
  9. }
  10. }
  11. function _errorHandler($err)
  12. {
  13. global $options;
  14.  
  15. $display_errors = ini_get('display_errors');
  16.  
  17. if ($display_errors) {
  18. $lineFormat = $options['lineFormat'];
  19. $contextFormat = $options['contextFormat'];
  20.  
  21. $file = $err['context']['file'];
  22. $line = $err['context']['line'];
  23. $func = $err['context']['class'];
  24. $func .= $err['context']['type'];
  25. $func .= $err['context']['function'];
  26.  
  27. $context = sprintf($contextFormat, $file, $line, $func);
  28.  
  29. printf($lineFormat."<br />\n", ucfirst($err['level']), $err['message'], $context);
  30. }
  31. }
  32. $logger['push_callback'] = '_pushCallback';
  33. $logger['error_handler'] = '_errorHandler';
  34.  
  35. $options = array(
  36. 'lineFormat' => '<b>%1$s</b>: %2$s <hr>%3$s',
  37. 'contextFormat' => '<b>Function</b>: %3$s<br/><b>File</b>: %1$s<br/><b>Line</b>: %2$s'
  38. );
  39. $logger['handler']['display'] = array('conf' => $options);
  40.  
  41. $bar = new HTML_Progress($logger);
  42. $e = $bar->setAnimSpeed('100'); // < - - - will generate an API exception
  43.  
  44. if (is_object($e)) {
  45. if (is_a($e,'PEAR_Error')) {
  46. die('<h1>Catch PEAR_Error API exception</h1>'. $e->toString());
  47. }
  48. }
  49. if (HTML_Progress::hasErrors()) {
  50. $err = HTML_Progress::getError();
  51. echo '<pre>';
  52. print_r($err);
  53. echo '</pre>';
  54. die('<h1>Catch HTML_Progress exception</h1>');
  55. }
  56.  
  57. $e = $bar->setAnimSpeed(10000); // < - - - will generate an API error
  58.  
  59. if (is_object($e)) {
  60. if (is_a($e,'PEAR_Error')) {
  61. die('<h1>Catch PEAR_Error API error</h1>'. $e->toString());
  62. }
  63. }
  64. if (HTML_Progress::hasErrors()) {
  65. $err = HTML_Progress::getError();
  66. die('<h1>Catch HTML_Progress error</h1>'.$err['message']);
  67. }
  68. ?>

Lines 4 to 10 we defined our 'push_callback' routine to ignore all exception error.

Lines 11 to 29 we defined our custom 'error_handler' routine.

Line 39, only the display handler is defined with configuration options 'lineFormat' and 'contextFormat'.

Line 41, we informs the new instance of HTML_Progress to use this custom error handler.

Line 42, raise an exception that is ignored and code line 44 thru 55 do nothing. Next (line 57) raise a basic error that return NULL. So lines 59 to 63 are ignored, and only lines 64 to 67 do their job.

Finally result of this process give on web standard output (browser) :

Error: invalid input, parameter #1 "$delay" was expecting "less or equal 1000", instead got "10000" 
--------------------------------------------------------------------------------------------------- 
Function: html_progress->setanimspeed 
File: d:\php\pear\html_progress\tutorials\html_progress\examples\display_errors-p6.php 
Line: 57 

Catch HTML_Progress error 
invalid input, parameter #1 "$delay" was expecting "less or equal 1000", instead got "10000"

Prev Up Next
Progress Handler Getting Started Using Indeterminate Mode

Documentation generated on Sun, 12 Sep 2004 20:22:45 +0200 by phpDocumentor 1.3.0RC3