58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Framework\ErrorHandling\Handlers;
|
|
|
|
use App\Framework\Database\Exception\DatabaseException;
|
|
use App\Framework\Logging\Logger;
|
|
use App\Framework\Logging\ValueObjects\LogContext;
|
|
|
|
/**
|
|
* Handler for database-related errors
|
|
*
|
|
* Priority: HIGH - Database errors need immediate attention
|
|
*/
|
|
final readonly class DatabaseErrorHandler implements ErrorHandlerInterface
|
|
{
|
|
public function __construct(
|
|
private Logger $logger
|
|
) {}
|
|
|
|
public function canHandle(\Throwable $exception): bool
|
|
{
|
|
return $exception instanceof DatabaseException
|
|
|| $exception instanceof \PDOException;
|
|
}
|
|
|
|
public function handle(\Throwable $exception): HandlerResult
|
|
{
|
|
// Log database error with context
|
|
$this->logger->error('Database error occurred', LogContext::withData([
|
|
'exception_class' => get_class($exception),
|
|
'message' => $exception->getMessage(),
|
|
'code' => $exception->getCode()
|
|
]));
|
|
|
|
return HandlerResult::create(
|
|
handled: true,
|
|
message: 'A database error occurred',
|
|
data: [
|
|
'error_type' => 'database',
|
|
'retry_after' => 60 // Suggest retry after 60 seconds
|
|
],
|
|
statusCode: 500
|
|
);
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return 'database_error_handler';
|
|
}
|
|
|
|
public function getPriority(): ErrorHandlerPriority
|
|
{
|
|
return ErrorHandlerPriority::HIGH;
|
|
}
|
|
}
|