feat: implement exception handling system with error context and policies

This commit is contained in:
2025-11-01 15:46:43 +01:00
parent f3440dff0d
commit a441da37f6
35 changed files with 920 additions and 88 deletions

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Framework\ExceptionHandling;
use App\Framework\ExceptionHandling\Strategy\StrictErrorPolicy;
use ErrorException;
final readonly class ErrorHandler
{
public function __construct(
private ErrorHandlerStrategy $strategy = new StrictErrorPolicy,
)
{}
/**
* @throws ErrorException
*/
public function handle(
int $severity,
string $message,
?string $file = null,
?int $line = null,
): bool {
$context = ErrorContext::create(
severity : $severity,
message : $message,
file : $file,
line : $line,
isSuppressed: $this->isSuppressed($severity)
);
$decision = $this->strategy->handle($context);
return match($decision) {
ErrorDecision::HANDLED => true,
ErrorDecision::DEFER => false,
ErrorDecision::THROW => throw new ErrorException($message, 0, $severity, $file, $line),
};
}
private function isSuppressed($severity): bool
{
return !(error_reporting() & $severity);
}
}