├── README ├── build.properties ├── build.xml ├── phpunit.xml.dist ├── src ├── demo │ ├── errors │ │ ├── e_compile_error.php │ │ ├── e_core_error.php │ │ ├── e_error.php │ │ └── e_parse.php │ └── index.php └── lib │ └── php-error-handler.php └── tests └── GeneralTest.php /README: -------------------------------------------------------------------------------- 1 | a little php class which can provide you an easy way to handle errors (even non recoverable errors like E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR) 2 | it wraps the error to an ErrorException instance and throws the exception(for catchable errors) or calls the default exception handler (for fatal errors) 3 | 4 | if you create an instance from PHP_Error_Handler without any arguments, it will call the global error/exception handler method, if it's available. 5 | 6 | the first argument can be a callable variable, if set, that will be called instead of the global error/exception handler. 7 | 8 | the second argument will be interpreted as a boolean value: if is set to true, the errors will be converted to ErrorException and passed to the exception handler (either to the global, or the one passed as argument). 9 | 10 | the third argument will be interpreted as a boolean value: if is set to true, then the script will overwrite the global error handler if is set. (only required if $errorsToException is set to true and another error handler was set with set_error_handler) 11 | 12 | BEWARE: you cannot continue the execution of your script on such errors, but with this script, you can gracefully terminate 13 | 14 | see the src/demo.php for examples 15 | 16 | Requirements: php >= 5.2.6 -------------------------------------------------------------------------------- /build.properties: -------------------------------------------------------------------------------- 1 | source=${basedir}/src/lib -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ./tests 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 15 | 16 | ./src/lib 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/demo/errors/e_compile_error.php: -------------------------------------------------------------------------------- 1 | $bar = 123; 5 | 6 | -------------------------------------------------------------------------------- /src/demo/errors/e_parse.php: -------------------------------------------------------------------------------- 1 | errorHandler = $errorHandler; 13 | $this->errorsAsExceptions = (bool) $errorsAsExceptions; 14 | if($errorsAsExceptions){ 15 | $oldErrorHandler = set_error_handler(array($this, 'errorHandler')); 16 | if($oldErrorHandler && !$overwriteHandler) { 17 | restore_error_handler(); 18 | throw new InvalidArgumentException('error handler is already set'); 19 | } 20 | } 21 | register_shutdown_function(array($this, 'shutdownHandler')); 22 | } 23 | 24 | public function __destruct() { 25 | if($this->errorsAsExceptions){ 26 | restore_error_handler(); 27 | } 28 | $this->canceled = true; 29 | } 30 | 31 | public function shutdownHandler() { 32 | try{ 33 | $error = error_get_last(); 34 | if($error && !$this->canceled){ 35 | if($this->errorsAsExceptions) { 36 | if(!$this->errorHandler) { 37 | $this->errorHandler = set_exception_handler(array($this, 'errorHandler')); 38 | restore_error_handler(); 39 | if(!$this->errorHandler) { 40 | error_log('cannot find the exception handler for error: '.print_r($error, true)); 41 | return; 42 | } 43 | } 44 | 45 | call_user_func($this->errorHandler,new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])); 46 | } 47 | else { 48 | if(!$this->errorHandler) { 49 | $this->errorHandler = set_error_handler(array($this, 'errorHandler')); 50 | restore_error_handler(); 51 | if(!$this->errorHandler) { 52 | return; 53 | } 54 | } 55 | call_user_func($this->errorHandler, $error['type'], $error['message'], $error['file'], $error['line']); 56 | } 57 | } 58 | } 59 | catch(Exception $e){ 60 | error_log("exception cannot be thrown from the shutdownHandler:\n".print_r($e, true), 4); 61 | } 62 | } 63 | 64 | public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext=NULL) { 65 | throw New ErrorException($errstr, 0, $errno, $errfile, $errline); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /tests/GeneralTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(TRUE); 9 | } 10 | } --------------------------------------------------------------------------------