diff options
author | Arnold Daniels <arnold@jasny.net> | 2016-10-28 00:18:36 +0200 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2016-10-28 00:18:36 +0200 |
commit | 8bf861bf03eb6ea98d0af861a835e6cb29406078 (patch) | |
tree | 78f6327c5289ccd845dab63e284bbf658152bdc8 /src | |
parent | 414bce100e6fcc913bdb79fb21c553ebdea3a51e (diff) | |
download | error-handler-8bf861bf03eb6ea98d0af861a835e6cb29406078.zip error-handler-8bf861bf03eb6ea98d0af861a835e6cb29406078.tar.gz error-handler-8bf861bf03eb6ea98d0af861a835e6cb29406078.tar.bz2 |
Added `onFatalError` method
This allows you to set a callback for when the script dies because of a fatal error
Diffstat (limited to 'src')
-rw-r--r-- | src/ErrorHandler.php | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 1f24c53..799c45e 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -53,6 +53,11 @@ class ErrorHandler implements LoggerAwareInterface */ protected $reservedMemory; + /** + * @var callback + */ + protected $onFatalError; + /** * Set the logger for logging errors @@ -216,6 +221,7 @@ class ErrorHandler implements LoggerAwareInterface return $errorResponse; } + /** * Use the global error handler to convert E_USER_ERROR and E_RECOVERABLE_ERROR to an ErrorException */ @@ -247,6 +253,25 @@ class ErrorHandler implements LoggerAwareInterface } /** + * Set a callback for when the script dies because of a fatal, non-catchable error. + * The callback should have an `ErrorException` as only argument. + * + * @param callable $callback + * @param boolean $clearOutput Clear the output buffer before calling the callback + */ + public function onFatalError($callback, $clearOutput = false) + { + if (!$clearOutput) { + $this->onFatalError = $callback; + } else { + $this->onFatalError = function($error) use ($callback) { + $this->clearOutputBuffer(); + $callback($error); + }; + } + } + + /** * Use the global error handler */ protected function initErrorHandler() @@ -326,6 +351,10 @@ class ErrorHandler implements LoggerAwareInterface if ($err['type'] & $this->logErrorTypes) { $this->log($error); } + + if ($this->onFatalError) { + call_user_func($this->onFatalError, $error); + } } @@ -404,6 +433,17 @@ class ErrorHandler implements LoggerAwareInterface /** + * Clear and destroy all the output buffers + * @codeCoverageIgnore + */ + protected function clearOutputBuffer() + { + while (ob_get_level() > 0) { + ob_end_clean(); + } + } + + /** * Wrapper method for `error_reporting` * @codeCoverageIgnore * |