From 24418052ab921f9c05e8608461786872325a0394 Mon Sep 17 00:00:00 2001 From: Kevin Yarmak Date: Tue, 6 Jul 2021 02:43:19 -0700 Subject: [PATCH] Fix #1479 more verbose pdo driver missing errors --- phalcon | 7 +++ scripts/Phalcon/Builder/Model.php | 19 +++++-- .../Db/PDODriverNotFoundException.php | 53 +++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 scripts/Phalcon/Exception/Db/PDODriverNotFoundException.php diff --git a/phalcon b/phalcon index a4ed3aa26..d4c40db95 100755 --- a/phalcon +++ b/phalcon @@ -37,9 +37,11 @@ use Phalcon\Commands\Builtin\Controller; use Phalcon\Commands\Builtin\Serve; use Phalcon\Commands\Builtin\Console; use Phalcon\Exception as PhalconException; +use Phalcon\Exception\Db\PDODriverNotFoundException; use Phalcon\Commands\DotPhalconMissingException; use Phalcon\Events\Manager as EventsManager; + try { require dirname(__FILE__) . '/bootstrap/autoload.php'; @@ -84,6 +86,11 @@ try { } catch (PhalconException $e) { fwrite(STDERR, Color::error($e->getMessage()) . PHP_EOL); exit(1); +} catch (PDODriverNotFoundException $e) { + $e->writeNicelyFormattedErrorOutput(); + fwrite(STDERR, 'Backtrace:'. PHP_EOL); + fwrite(STDERR, $e->getTraceAsString() . PHP_EOL); + exit(1); } catch (Exception $e) { fwrite(STDERR, 'ERROR: ' . $e->getMessage() . PHP_EOL); exit(1); diff --git a/scripts/Phalcon/Builder/Model.php b/scripts/Phalcon/Builder/Model.php index a5422474e..ba4765531 100644 --- a/scripts/Phalcon/Builder/Model.php +++ b/scripts/Phalcon/Builder/Model.php @@ -21,6 +21,7 @@ namespace Phalcon\Builder; +use PDOException; use Phalcon\Text; use Phalcon\Utils; use ReflectionClass; @@ -34,8 +35,10 @@ use Phalcon\Exception\InvalidArgumentException; use Phalcon\Options\OptionsAware as ModelOption; use Phalcon\Exception\InvalidParameterException; +use Phalcon\Exception\Db\PDODriverNotFoundException; use Phalcon\Validation\Validator\Email as EmailValidator; + /** * ModelBuilderComponent * @@ -191,9 +194,19 @@ public function build() $adapterName = 'Phalcon\Db\Adapter\Pdo\\' . $adapter; unset($configArray['adapter']); - /** @var Pdo $db */ - $db = new $adapterName($configArray); - + try { + /** @var Pdo $db */ + $db = new $adapterName($configArray); + } catch(PDOException $e){ + switch($e->getMessage()) + { + case 'could not find driver': + throw new PDODriverNotFoundException("PDO could not find driver $adapter", $adapter); + break; + default: + throw new PDOException($e->getMessage()); + } + } $initialize = []; if ($this->shouldInitSchema()) { diff --git a/scripts/Phalcon/Exception/Db/PDODriverNotFoundException.php b/scripts/Phalcon/Exception/Db/PDODriverNotFoundException.php new file mode 100644 index 000000000..46a3f0539 --- /dev/null +++ b/scripts/Phalcon/Exception/Db/PDODriverNotFoundException.php @@ -0,0 +1,53 @@ + | + +------------------------------------------------------------------------+ +*/ + +namespace Phalcon\Exception\Db; + +use PDOException; +use Phalcon\Script\Color; +use PDO; + +class PDODriverNotFoundException extends PDOException +{ + protected $adapter = ''; + + public function __construct($message, $adapter = '') + { + parent::__construct($message); + $this->adapter = $adapter; + } + + public function getAdapter() + { + return $this->adapter; + } + + public function writeNicelyFormattedErrorOutput() + { + fwrite(STDERR, Color::error($this->getMessage()) . PHP_EOL); + + if (!extension_loaded('PDO')) { + fwrite(STDERR, Color::error('PDO extension is not loaded') . PHP_EOL); + } else { + $loadedDrivers = PDO::getAvailableDrivers(); + fwrite(STDERR, 'PDO Drivers loaded:' . PHP_EOL); + fwrite(STDERR, print_r($loadedDrivers, true). PHP_EOL); + } + } +}