PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/Base/WHErrorHandler.php

http://phaux.googlecode.com/
PHP | 141 lines | 102 code | 23 blank | 16 comment | 4 complexity | 7f77d90dfba985cf9b2520486ffec829 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Base
  4. */
  5. $WHERROR_TYPES = array (
  6. E_ERROR => 'Error',
  7. E_WARNING => 'Warning',
  8. E_PARSE => 'Parsing Error',
  9. E_NOTICE => 'Notice',
  10. E_CORE_ERROR => 'Core Error',
  11. E_CORE_WARNING => 'Core Warning',
  12. E_COMPILE_ERROR => 'Compile Error',
  13. E_COMPILE_WARNING => 'Compile Warning',
  14. E_USER_ERROR => 'User Error',
  15. E_USER_WARNING => 'User Warning',
  16. E_USER_NOTICE => 'User Notice',
  17. E_STRICT => 'Runtime Notice',
  18. E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
  19. );
  20. $WHERROR_FATAL = array(
  21. E_ERROR,
  22. E_WARNING,
  23. E_PARSE,
  24. E_CORE_ERROR,
  25. E_COMPILE_ERROR,
  26. E_USER_ERROR,
  27. E_RECOVERABLE_ERROR
  28. );
  29. /*
  30. ** Can't handle E_ERROR, E_PARSE, E_CORE_ERROR,
  31. ** E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
  32. ** But I am including them in case some time in
  33. ** the future this changes
  34. */
  35. $WHERROR_TRACEBACK = array(
  36. E_ERROR,
  37. E_USER_ERROR,
  38. E_USER_WARNING,
  39. E_RECOVERABLE_ERROR,
  40. );
  41. $WHERROR_CATCH = E_ALL;
  42. /*
  43. **An array of non fatal errors thrown
  44. ** The reason we need to use a global here is
  45. ** because you can not register an error handler
  46. ** on an object
  47. */
  48. $WHERROR_NON_FATAL_ERRORS = array();
  49. class WHErrorHandler extends Object {
  50. protected $cleanExit = FALSE;
  51. protected $errorUrl = "/error.php";
  52. protected $nonFatalErrors = array();
  53. public function start(){
  54. $this->registerErrorHandler();
  55. ob_start(array($this, 'checkForErrorsAndOutput'));
  56. return $this;
  57. }
  58. static function registerErrorHandler(){
  59. global $WHERROR_CATCH;
  60. set_error_handler(array("WHErrorHandler","errorHandler"));
  61. error_reporting($WHERROR_CATCH);
  62. }
  63. static function errorHandler($errno,$errstr,$errfile,$errline){
  64. global $WHERROR_FATAL;
  65. global $WHERROR_TYPES;
  66. global $DEBUG_ERRORS;
  67. global $WHERROR_TRACEBACK;
  68. global $WHERROR_NON_FATAL_ERRORS;
  69. //die($errstr."\n");
  70. if(in_array($errno,$WHERROR_FATAL)){
  71. if(in_array($errno,$WHERROR_TRACEBACK)){
  72. throw new WHException($WHERROR_TYPES[$errno], $errno);
  73. }else{
  74. die("$errstr in $errfile on line $errline");
  75. }
  76. }else{
  77. $WHERROR_NON_FATAL_ERRORS[] = Object::construct('WHError')->
  78. setNumber($errno)->
  79. setString($errstr)->
  80. setFile($errfile)->
  81. setLine($errline);
  82. }
  83. }
  84. static function nonFatalErrorsThrown(){
  85. global $WHERROR_NON_FATAL_ERRORS;
  86. return $WHERROR_NON_FATAL_ERRORS;
  87. }
  88. public function pretyError($errno,$errstr,$errfile,$errline){
  89. $return = "<h2> Error: #$errno ".
  90. $errstr.
  91. " in ".
  92. $errfile.
  93. " on line " .
  94. $errline.
  95. "</h2>";
  96. foreach(debug_backtrace() as $point){
  97. $return .= WHException::niceFromTracePoint($point,TRUE);
  98. }
  99. return substr($return,0,1000000);
  100. }
  101. public function checkForErrorsAndOutput($buffer){
  102. global $app;
  103. global $app_configurations;
  104. if(!$this->cleanExit){
  105. if(!$app_configurations[$app]['general']['debug']){
  106. $message = " # ". WHException::writeErrorToTempFile($buffer);
  107. }else{
  108. $message = $buffer;
  109. }
  110. return WHException::errorPage($message,$app_configurations[$app]['general']['admin_email']);
  111. }
  112. return $buffer;
  113. }
  114. public function end(){
  115. $this->cleanExit = TRUE;
  116. return $this;
  117. }
  118. }