Improve clarity of the error that happens when MariaDB fails to boot.

This commit is contained in:
Buster Neece 2022-11-23 06:40:31 -06:00
parent 4096c9d2a5
commit fe4849521f
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
2 changed files with 49 additions and 42 deletions

View File

@ -6,7 +6,6 @@
use App\Environment;
use App\Event;
use App\Exception;
use Psr\Container\ContainerInterface;
return [
@ -102,55 +101,45 @@ return [
]
);
try {
$mappingClassesPaths = [$environment->getBaseDirectory() . '/src/Entity'];
$mappingClassesPaths = [$environment->getBaseDirectory() . '/src/Entity'];
$buildDoctrineMappingPathsEvent = new Event\BuildDoctrineMappingPaths(
$mappingClassesPaths,
$environment->getBaseDirectory()
);
$dispatcher->dispatch($buildDoctrineMappingPathsEvent);
$buildDoctrineMappingPathsEvent = new Event\BuildDoctrineMappingPaths(
$mappingClassesPaths,
$environment->getBaseDirectory()
);
$dispatcher->dispatch($buildDoctrineMappingPathsEvent);
$mappingClassesPaths = $buildDoctrineMappingPathsEvent->getMappingClassesPaths();
$mappingClassesPaths = $buildDoctrineMappingPathsEvent->getMappingClassesPaths();
// Fetch and store entity manager.
$config = Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration(
$mappingClassesPaths,
!$environment->isProduction(),
$environment->getTempDirectory() . '/proxies',
$psr6Cache
);
// Fetch and store entity manager.
$config = Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration(
$mappingClassesPaths,
!$environment->isProduction(),
$environment->getTempDirectory() . '/proxies',
$psr6Cache
);
$config->setAutoGenerateProxyClasses(
Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED
);
$config->setAutoGenerateProxyClasses(
Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED
);
// Debug mode:
// $config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger);
// Debug mode:
// $config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger);
$config->addCustomNumericFunction('RAND', DoctrineExtensions\Query\Mysql\Rand::class);
$config->addCustomNumericFunction('RAND', DoctrineExtensions\Query\Mysql\Rand::class);
if (!Doctrine\DBAL\Types\Type::hasType('carbon_immutable')) {
Doctrine\DBAL\Types\Type::addType('carbon_immutable', Carbon\Doctrine\CarbonImmutableType::class);
}
$eventManager = new Doctrine\Common\EventManager();
$eventManager->addEventSubscriber($eventRequiresRestart);
$eventManager->addEventSubscriber($eventAuditLog);
$eventManager->addEventSubscriber($eventChangeTracking);
return new App\Doctrine\DecoratedEntityManager(
function () use (
$connectionOptions,
$config,
$eventManager
) {
return Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager);
}
);
} catch (Exception $e) {
throw new App\Exception\BootstrapException($e->getMessage());
if (!Doctrine\DBAL\Types\Type::hasType('carbon_immutable')) {
Doctrine\DBAL\Types\Type::addType('carbon_immutable', Carbon\Doctrine\CarbonImmutableType::class);
}
$eventManager = new Doctrine\Common\EventManager();
$eventManager->addEventSubscriber($eventRequiresRestart);
$eventManager->addEventSubscriber($eventAuditLog);
$eventManager->addEventSubscriber($eventChangeTracking);
return new App\Doctrine\DecoratedEntityManager(
fn() => Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager)
);
},
App\Doctrine\ReloadableEntityManagerInterface::class => DI\Get(App\Doctrine\DecoratedEntityManager::class),

View File

@ -6,6 +6,7 @@ namespace App\Doctrine;
use App\Entity\Interfaces\IdentifiableEntityInterface;
use Closure;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\ORMInvalidArgumentException;
@ -16,6 +17,23 @@ final class DecoratedEntityManager extends EntityManagerDecorator implements Rel
public function __construct(callable $createEm)
{
parent::__construct($createEm());
try {
$this->getConnection()->getNativeConnection();
} catch (ConnectionException $e) {
if (2002 === $e->getCode()) {
throw new \Exception(
sprintf(
'Could not connect to the database. Check the error log for additional information. (%s)',
$e->getMessage()
),
$e->getCode()
);
}
throw $e;
}
$this->createEm = $createEm(...);
}