Skip to content

Commit

Permalink
Merge pull request #1511 from Ultimater/fix-1479-pdo-adapter-error-4.1.x
Browse files Browse the repository at this point in the history
Fix #1479 more verbose pdo driver missing errors for 4.1.x
  • Loading branch information
Ultimater authored Jul 7, 2021
2 parents 3df8268 + 90db673 commit 5cf8c9a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions phalcon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use Phalcon\DevTools\Commands\Builtin\Serve;
use Phalcon\DevTools\Commands\Builtin\Webtools;
use Phalcon\DevTools\Commands\CommandsListener;
use Phalcon\DevTools\Commands\DotPhalconMissingException;
use Phalcon\DevTools\Exception\PDODriverNotFoundException;
use Phalcon\DevTools\Script;
use Phalcon\DevTools\Script\Color;
use Phalcon\DevTools\Version;
Expand Down Expand Up @@ -72,6 +73,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);
Expand Down
16 changes: 14 additions & 2 deletions src/Builder/Component/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Phalcon\DevTools\Exception\InvalidParameterException;
use Phalcon\DevTools\Exception\RuntimeException;
use Phalcon\DevTools\Exception\WriteFileException;
use Phalcon\DevTools\Exception\PDODriverNotFoundException;
use Phalcon\DevTools\Generator\Snippet;
use Phalcon\DevTools\Options\OptionsAware as ModelOption;
use Phalcon\DevTools\Utils;
Expand All @@ -29,6 +30,7 @@
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionProperty;
use PDOException;

/**
* Builder to generate models
Expand Down Expand Up @@ -138,8 +140,18 @@ public function build(): void
$adapterName = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;
unset($configArray['adapter']);

/** @var AbstractPdo $db */
$db = new $adapterName($configArray);
try {
/** @var AbstractPdo $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 = [];

Expand Down
46 changes: 46 additions & 0 deletions src/Exception/PDODriverNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Developer Tools.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Phalcon\DevTools\Exception;

use PDOException;
use Phalcon\Devtools\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);
}
}
}

0 comments on commit 5cf8c9a

Please sign in to comment.