Examples TOCexamples

Display handler - part 6.

$Date: 2004/08/07 14:28:59 $

 Table of contents

Introduction

This example requires :


This example will show you how to customize display errors renderers.

An error was volontary made on call of setAnimSpeed method (lines 41 and 56). We have decided to ignore API exception (lines 4 thru 10 and 30), and use our own error handler rather than PEAR_Error (lines 11 thru 29 and 31). Display handler renderer was changed on lines 33 thru 37. So only lines 62 to 65 will be executed to catch HTML_Progress error.

display_errors

Print out errors (as a part of the output). For production web sites, you're strongly encouraged to turn this feature off, and use error logging instead. Keeping display_errors enabled on a production web site may reveal security information to end users, such as file paths on your Web server, your database schema or other information.

1 <?php ini_set('display_errors', false); ?>

log_errors

Log errors into a log file (server-specific log, stderr, syslog ...) As stated above, you're strongly advised to use error logging in place of error displaying on production web sites.

1 <?php ini_set('log_errors', true); ?>

 Render options

Default options below are used :

Only option changed is :

Legend for lineFormat: Legend for contextFormat:

[Top]

 Output

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\examples\errorstack\secure\display_errors-p6.php
Line: 55

Catch HTML_Progress error

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

[Top]

 PHP source syntax highlight

  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 = isset($err['context']['file']) ? $err['context']['file'] : '';
  22.             $line = isset($err['context']['line']) ? $err['context']['line'] : '';
  23.             $func = isset($err['context']['function']) ? $err['context']['class'].'::'.$err['context']['function'] : '';
  24.  
  25.             $context = sprintf($contextFormat, $file, $line, $func);
  26.            
  27.             printf($lineFormat."<br />\n", ucfirst($err['level']), $err['message'], $context);
  28.         }
  29. }
  30. $logger['push_callback'] = '_pushCallback';
  31. $logger['error_handler'] = '_errorHandler';
  32.  
  33. $options = array(
  34.     'lineFormat' => '<b>%1$s</b>: %2$s <hr>%3$s',
  35.     'contextFormat' => '<b>Function</b>: %3$s<br/><b>File</b>: %1$s<br/><b>Line</b>: %2$s'
  36. );
  37. $logger['handler']['display'] = array('conf' => $options);  
  38.  
  39. $bar = new HTML_Progress($logger);
  40. $e = $bar->setAnimSpeed('100');   // < - - - will generate an API exception
  41.  
  42. if (is_object($e)) {
  43.     if (is_a($e,'PEAR_Error')) {
  44.         die('<h1>Catch PEAR_Error API exception</h1>'. $e->toString());
  45.     }
  46. }
  47. if ($bar->hasErrors()) {
  48.     $err = $bar->getError();
  49.     echo '<pre>';
  50.     print_r($err);
  51.     echo '</pre>';
  52.     die('<h1>Catch HTML_Progress exception</h1>');
  53. }
  54.  
  55. $e = $bar->setAnimSpeed(10000);   // < - - - will generate an API error
  56.  
  57. if (is_object($e)) {
  58.     if (is_a($e,'PEAR_Error')) {
  59.         die('<h1>Catch PEAR_Error API error</h1>'. $e->toString());
  60.     }
  61. }
  62. if ($bar->hasErrors()) {
  63.     $err = $bar->getError();
  64.     die('<h1>Catch HTML_Progress error</h1>'.$err['message']);
  65. }
  66. ?>

[Top]