feat: implement exception handling system with error context and policies
This commit is contained in:
56
src/Framework/ExceptionHandling/ShutdownHandler.php
Normal file
56
src/Framework/ExceptionHandling/ShutdownHandler.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\ExceptionHandling;
|
||||
|
||||
use App\Framework\ErrorHandling\ErrorHandlerManager;
|
||||
use App\Framework\ExceptionHandling\Strategy\StrictErrorPolicy;
|
||||
use Error;
|
||||
|
||||
final readonly class ShutdownHandler
|
||||
{
|
||||
private const array FATAL_TYPES = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$last = error_get_last();
|
||||
|
||||
if (!$last || !$this->isFatalError($last['type'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->cleanOutputBuffer();
|
||||
|
||||
$file = (string)($last['file'] ?? 'unknown');
|
||||
$line = (int)($last['line'] ?? 0);
|
||||
|
||||
$error = new Error($last['message'] ?? 'Fatal error',0);
|
||||
|
||||
|
||||
|
||||
try {
|
||||
$ehm = new ErrorKernel();
|
||||
$ehm->handle($error, ['file' => $file, 'line' => $line]);
|
||||
} catch (\Throwable) {
|
||||
|
||||
}
|
||||
|
||||
exit(255);
|
||||
}
|
||||
|
||||
private function cleanOutputBuffer(): void
|
||||
{
|
||||
try {
|
||||
while (ob_get_level() > 0) {
|
||||
@ob_end_clean();
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
private function isFatalError(?int $type = null): bool
|
||||
{
|
||||
return in_array($type ?? 0, self::FATAL_TYPES, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user