Skip to content

Commit

Permalink
Merge pull request #1509 from Ultimater/fix-1479-pdo-adapter-error-3.4.x
Browse files Browse the repository at this point in the history
Fix #1479 more verbose pdo driver missing errors for 3.4.x
  • Loading branch information
Ultimater authored Jul 6, 2021
2 parents 1af396c + 2441805 commit b3ae6bd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
7 changes: 7 additions & 0 deletions phalcon
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
19 changes: 16 additions & 3 deletions scripts/Phalcon/Builder/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Phalcon\Builder;

use PDOException;
use Phalcon\Text;
use Phalcon\Utils;
use ReflectionClass;
Expand All @@ -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
*
Expand Down Expand Up @@ -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()) {
Expand Down
53 changes: 53 additions & 0 deletions scripts/Phalcon/Exception/Db/PDODriverNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-present Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Kevin Yarmak <[email protected]> |
+------------------------------------------------------------------------+
*/

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);
}
}
}

0 comments on commit b3ae6bd

Please sign in to comment.