-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,10 +41,17 @@ final class ErrorHandler | |
private bool $enabled = false; | ||
private bool $initialized = false; | ||
|
||
/** | ||
* @param LoggerInterface $logger Logger to write errors to. | ||
* @param ThrowableRendererInterface $defaultRenderer Default throwable renderer. | ||
* @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. | ||
* @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. | ||
*/ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
private ThrowableRendererInterface $defaultRenderer, | ||
private ?EventDispatcherInterface $eventDispatcher = null, | ||
private int $exitShutdownHandlerDepth = 2 | ||
Check warning on line 54 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 54 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 54 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 54 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
) { | ||
} | ||
|
||
|
@@ -108,7 +115,7 @@ public function register(): void | |
|
||
$this->initializeOnce(); | ||
Check warning on line 116 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 116 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
|
||
// Handles throwable, echo output and exit. | ||
// Handles throwable that isn't caught otherwise, echo output and exit. | ||
set_exception_handler(function (Throwable $t): void { | ||
Check warning on line 119 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
Check warning on line 119 in src/ErrorHandler.php GitHub Actions / mutation / PHP 8.1-ubuntu-latest
|
||
if (!$this->enabled) { | ||
return; | ||
|
@@ -200,16 +207,41 @@ private function renderThrowableAndTerminate(Throwable $t): void | |
if (!empty($this->workingDirectory)) { | ||
chdir($this->workingDirectory); | ||
} | ||
// disable error capturing to avoid recursive errors while handling exceptions | ||
// Disable error capturing to avoid recursive errors while handling exceptions. | ||
$this->unregister(); | ||
// set preventive HTTP status code to 500 in case error handling somehow fails and headers are sent | ||
// Set preventive HTTP status code to 500 in case error handling somehow fails and headers are sent. | ||
http_response_code(Status::INTERNAL_SERVER_ERROR); | ||
|
||
echo $this->handle($t); | ||
$this->eventDispatcher?->dispatch(new ApplicationError($t)); | ||
|
||
register_shutdown_function(static function (): void { | ||
exit(1); | ||
}); | ||
$handler = $this->wrapShutdownHandler( | ||
static function (): void { | ||
exit(1); | ||
}, | ||
$this->exitShutdownHandlerDepth | ||
); | ||
|
||
register_shutdown_function($handler); | ||
} | ||
|
||
/** | ||
* Wraps shutdown handler into another shutdown handler to ensure it is called last after all other shutdown | ||
* functions, even those added to the end. | ||
* | ||
* @param callable $handler Shutdown handler to wrap. | ||
* @param int $depth Wrapping depth. | ||
* @return callable Wrapped handler. | ||
*/ | ||
private function wrapShutdownHandler(callable $handler, int $depth): callable | ||
{ | ||
$currentDepth = 0; | ||
while ($currentDepth < $depth) { | ||
$handler = static function() use ($handler): void { | ||
register_shutdown_function($handler); | ||
}; | ||
$currentDepth++; | ||
} | ||
return $handler; | ||
} | ||
} |