From 981cc588a6e103614d09d734c5205872337edb4d Mon Sep 17 00:00:00 2001 From: Rafal Lindemann Date: Fri, 1 Feb 2013 09:46:10 +0100 Subject: [PATCH] addErrorCallback() will add a callback to call just right after logging the error in reportError. It might be used to use additional loggers, or to display a nice error page for non-development users. --- src/php_error.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/php_error.php b/src/php_error.php index fa55cd5..4cd145b 100644 --- a/src/php_error.php +++ b/src/php_error.php @@ -1135,6 +1135,8 @@ public static function identifyTypeHTML( $arg, $recurseLevels=1 ) { private $classNotFoundException; + private $callbacks = array(); + /** * = Options = * @@ -2355,6 +2357,12 @@ public function reportError( $code, $message, $errLine, $errFile, $ex=null ) { $this->logError( $message, $errFile, $errLine, $ex ); + if ($this->triggerCallback( $code, $message, $errLine, $errFile, $ex )) { + // exit in order to end processing + $this->turnOff(); + exit(0); + } + /** * It runs if: * - it is globally enabled @@ -2427,6 +2435,32 @@ public function reportError( $code, $message, $errLine, $errFile, $ex=null ) { } } + protected function triggerCallback( $code, $message, $errLine, $errFile, $ex=null ) { + foreach($this->callbacks as $callback) { + if (call_user_func_array($callback, func_get_args())) return true; + } + } + + /** Adds a callback that will be called just right after logging + * + * Callback will receive the same arguments as reportError: + * callback($code, $message, $errLine, $errFile, $ex) + * + * If the callback returns anything, no other callback will be called and the script + * will exit before displaying the error. + * + */ + public function addErrorCallback($callback) { + if (is_callable($callback) == false) throw new Exception('Callback not callable!'); + $this->callbacks[] = $callback; + } + + public function removeErrorCallback($callback) { + $key = array_search($callback, $this->callbacks, true); + if ($key === false) throw new Exception('Callback not set!'); + unset($this->callbacks[$key]); + } + private function getStackTrace( $ex, $code, $errFile, $errLine ) { $stackTrace = null;