Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/php_error.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,8 @@ public static function identifyTypeHTML( $arg, $recurseLevels=1 ) {

private $classNotFoundException;

private $callbacks = array();

/**
* = Options =
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down