Skip to content

Commit

Permalink
Merge pull request #3 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix to Bug When Code Execution Stopped By Another Handler
  • Loading branch information
usernane authored Oct 18, 2022
2 parents a3e8d8f + 80ff189 commit 44102a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
25 changes: 25 additions & 0 deletions src/webfiori/error/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ abstract class AbstractHandler {
private $traceArr;
private $name;
private $isCalled;
private $isExecuting;
/**
* Creates new instance of the class.
*/
public function __construct() {
$this->traceArr = [];
$this->name = 'New Handler';
$this->isCalled = false;
$this->isExecuting = false;
}
/**
* Sets the handler as executed.
Expand Down Expand Up @@ -134,6 +136,29 @@ public function setException(Throwable $ex) {
$this->exception = $ex;
$this->setTrace();
}
/**
* Sets the value that tells if the handler is begin executed or not.
*
* This method is used internally by the library to set status of the
* handler.
*
* @param bool $isExec True to set the handler as begin executed. False
* to not.
*/
public function setIsExecuting(bool $isExec) {
$this->isExecuting = $isExec;
}
/**
* Check if the handler is in execution stage or not.
*
* This method is used to indicate if execution
* scope is inside the method AbstractHandler::handle() or not.
*
* @return bool True if the handler is executing. False if not.
*/
public function isExecuting() : bool {
return $this->isExecuting;
}
private function setTrace() {
$ex = $this->getException();

Expand Down
23 changes: 13 additions & 10 deletions src/webfiori/error/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ class Handler {
*
* @var AbstractHandler
*/
private $handler;
private $isErrOccured;
/**
*
* @var Throwable|null
*/
private $lastException;
/**
*
* @var Handler
Expand All @@ -91,6 +96,7 @@ private function __construct() {
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
$this->isErrOccured = false;
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline)
{
$errClass = TraceEntry::extractClassName($errfile);
Expand All @@ -100,30 +106,27 @@ private function __construct() {
});
set_exception_handler(function (Throwable $ex)
{
$this->lastException = $ex;
foreach (Handler::get()->handlersPool as $h) {

if ($h->isActive()) {
$h->setException($ex);
$h->setIsExecuting(true);
$h->handle();
$h->setIsExecuting(false);
$h->setIsExecuted(true);
}
}
});
register_shutdown_function(function () {
$lastErr = error_get_last();

if ($lastErr !== null) {
if ($this->lastException !== null) {
if (ob_get_length()) {
ob_clean();
}
$errClass = TraceEntry::extractClassName($lastErr['file']);
$errType = Handler::ERR_TYPES[$lastErr['type']];
$message = $errType['description'].': '.$lastErr['message'].' At '.$errClass.' Line '.$lastErr['line'];
$ex = new ErrorHandlerException($message, $lastErr['type'], $lastErr['file']);
foreach (Handler::get()->handlersPool as $h) {

if ($h->isActive() && $h->isShutdownHandler() && !$h->isExecuted()) {
$h->setException($ex);
if ($h->isActive() && $h->isShutdownHandler() && !$h->isExecuted() && !$h->isExecuting()) {
$h->setException($this->lastException);
$h->handle();
$h->setIsExecuted(true);
}
Expand Down

0 comments on commit 44102a0

Please sign in to comment.