Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to Bug When Code Execution Stopped By Another Handler #3

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
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
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