PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/bundle/PIV/doc/set-error-handler.txt

https://bitbucket.org/exu/vim
Plain Text | 208 lines | 168 code | 40 blank | 0 comment | 0 complexity | 8efcb214dfdbf3f17afb1e62f83599e7 MD5 | raw file
Possible License(s): AGPL-1.0
  1. *set_error_handler* -- Sets a user-defined error handler function
  2. mixed set_error_handler(callback error_handler [, int error_types])~
  3. Sets a user function ({error_handler}) to handle errors in a script.
  4. This function can be used for defining your own way of handling errors during
  5. runtime, for example in applications in which you need to do cleanup of
  6. data/files when a critical error happens, or when you need to trigger an error
  7. under certain conditions (using |trigger_error|).
  8. It is important to remember that the standard PHP error handler is completely
  9. bypassed for the error types specified by {error_types} unless the callback
  10. function returns FALSE. |error_reporting| settings will have no effect and
  11. your error handler will be called regardless - however you are still able to
  12. read the current value of error_reporting and act appropriately. Of particular
  13. note is that this value will be 0 if the statement that caused the error was
  14. prepended by the @ error-control operator.
  15. Also note that it is your responsibility to |die| if necessary. If the
  16. error-handler function returns, script execution will continue with the next
  17. statement after the one that caused an error.
  18. The following error types cannot be handled with a user defined function:
  19. E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
  20. E_COMPILE_WARNING, and most of E_STRICT raised in the file where
  21. |set_error_handler| is called.
  22. If errors occur before the script is executed (e.g. on file uploads) the
  23. custom error handler cannot be called since it is not registered at that time.
  24. {error_handler} The user function needs to accept two parameters: the error
  25. code, and a string describing the error. Then there are three optional
  26. parameters that may be supplied: the filename in which the error occurred, the
  27. line number in which the error occurred, and the context in which the error
  28. occurred (an array that points to the active symbol table at the point the
  29. error occurred). The function can be shown as:
  30. mixed (callback error_handler [, int error_types, int errno, string errstr [, string errfile [, int errline [, array errcontext]]]])~
  31. {errno} The first parameter, {errno}, contains the level of the error raised,
  32. as an integer.
  33. {errstr} The second parameter, {errstr}, contains the error message, as a
  34. string.
  35. {errfile} The third parameter is optional, {errfile}, which contains the
  36. filename that the error was raised in, as a string.
  37. {errline} The fourth parameter is optional, {errline}, which contains the line
  38. number the error was raised at, as an integer.
  39. {errcontext} The fifth parameter is optional, {errcontext}, which is an array
  40. that points to the active symbol table at the point the error occurred. In
  41. other words, {errcontext} will contain an array of every variable that existed
  42. in the scope the error was triggered in. User error handler must not modify
  43. error context.
  44. If the function returns FALSE then the normal error handler continues.
  45. {error_types} Can be used to mask the triggering of the {error_handler}
  46. function just like the error_reporting ini setting controls which errors are
  47. shown. Without this mask set the {error_handler} will be called for every
  48. error regardless to the setting of the error_reporting setting.
  49. Returns a string containing the previously defined error handler (if any). If
  50. the built-in error handler is used NULL is returned. NULL is also returned in
  51. case of an error such as an invalid callback. If the previous error handler
  52. was a class method, this function will return an indexed array with the class
  53. and the method name.
  54. Version Description 5.2.0 The error handler must return FALSE to populate
  55. $php_errormsg. 5.0.0 The {error_types} parameter was introduced. 4.3.0 Instead
  56. of a function name, an array containing an object reference and a method name
  57. can also be supplied as the {error_handler}. 4.0.2 Three optional parameters
  58. for the {error_handler} user function was introduced. These are the filename,
  59. the line number, and the context.
  60. Error handling with |set_error_handler| and |trigger_error| The example below
  61. shows the handling of internal exceptions by triggering errors and handling
  62. them with a user defined function:
  63. <?php >
  64. // error handler function
  65. function myErrorHandler($errno, $errstr, $errfile, $errline)
  66. {
  67. if (!(error_reporting() &amp; $errno)) {
  68. // This error code is not included in error_reporting
  69. return;
  70. }
  71. switch ($errno) {
  72. case E_USER_ERROR:
  73. echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
  74. echo " Fatal error on line $errline in file $errfile";
  75. echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
  76. echo "Aborting...<br />\n";
  77. exit(1);
  78. break;
  79. case E_USER_WARNING:
  80. echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
  81. break;
  82. case E_USER_NOTICE:
  83. echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
  84. break;
  85. default:
  86. echo "Unknown error type: [$errno] $errstr<br />\n";
  87. break;
  88. }
  89. /* Don't execute PHP internal error handler */
  90. return true;
  91. }
  92. // function to test the error handling
  93. function scale_by_log($vect, $scale)
  94. {
  95. if (!is_numeric($scale) || $scale <= 0) {
  96. trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
  97. }
  98. if (!is_array($vect)) {
  99. trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
  100. return null;
  101. }
  102. $temp = array();
  103. foreach($vect as $pos => $value) {
  104. if (!is_numeric($value)) {
  105. trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
  106. $value = 0;
  107. }
  108. $temp[$pos] = log($scale) * $value;
  109. }
  110. return $temp;
  111. }
  112. // set to the user defined error handler
  113. $old_error_handler = set_error_handler("myErrorHandler");
  114. // trigger some errors, first define a mixed array with a non-numeric item
  115. echo "vector a\n";
  116. $a = array(2, 3, "foo", 5.5, 43.3, 21.11);
  117. print_r($a);
  118. // now generate second array
  119. echo "----\nvector b - a notice (b = log(PI) * a)\n";
  120. /* Value at position $pos is not a number, using 0 (zero) */
  121. $b = scale_by_log($a, M_PI);
  122. print_r($b);
  123. // this is trouble, we pass a string instead of an array
  124. echo "----\nvector c - a warning\n";
  125. /* Incorrect input vector, array of values expected */
  126. $c = scale_by_log("not array", 2.3);
  127. var_dump($c); // NULL
  128. // this is a critical error, log of zero or negative number is undefined
  129. echo "----\nvector d - fatal error\n";
  130. /* log(x) for x <= 0 is undefined, you used: scale = $scale" */
  131. $d = scale_by_log($a, -2.5);
  132. var_dump($d); // Never reached
  133. ?>
  134. The above example will output something similar to:
  135. vector a
  136. Array
  137. (
  138. [0] => 2
  139. [1] => 3
  140. [2] => foo
  141. [3] => 5.5
  142. [4] => 43.3
  143. [5] => 21.11
  144. )
  145. ----
  146. vector b - a notice (b = log(PI) * a)
  147. <b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
  148. Array
  149. (
  150. [0] => 2.2894597716988
  151. [1] => 3.4341896575482
  152. [2] => 0
  153. [3] => 6.2960143721717
  154. [4] => 49.566804057279
  155. [5] => 24.165247890281
  156. )
  157. ----
  158. vector c - a warning
  159. <b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />
  160. NULL
  161. ----
  162. vector d - fatal error
  163. <b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br />
  164. Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br />
  165. Aborting...<br />
  166. ErrorException |error_reporting| |restore_error_handler| |trigger_error| error
  167. level constants information about the callback type
  168. vim:ft=help: