Skip to content

Commit b3ae6bd

Browse files
authored
Merge pull request #1509 from Ultimater/fix-1479-pdo-adapter-error-3.4.x
Fix #1479 more verbose pdo driver missing errors for 3.4.x
2 parents 1af396c + 2441805 commit b3ae6bd

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

phalcon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ use Phalcon\Commands\Builtin\Controller;
3737
use Phalcon\Commands\Builtin\Serve;
3838
use Phalcon\Commands\Builtin\Console;
3939
use Phalcon\Exception as PhalconException;
40+
use Phalcon\Exception\Db\PDODriverNotFoundException;
4041
use Phalcon\Commands\DotPhalconMissingException;
4142
use Phalcon\Events\Manager as EventsManager;
4243

44+
4345
try {
4446
require dirname(__FILE__) . '/bootstrap/autoload.php';
4547

@@ -84,6 +86,11 @@ try {
8486
} catch (PhalconException $e) {
8587
fwrite(STDERR, Color::error($e->getMessage()) . PHP_EOL);
8688
exit(1);
89+
} catch (PDODriverNotFoundException $e) {
90+
$e->writeNicelyFormattedErrorOutput();
91+
fwrite(STDERR, 'Backtrace:'. PHP_EOL);
92+
fwrite(STDERR, $e->getTraceAsString() . PHP_EOL);
93+
exit(1);
8794
} catch (Exception $e) {
8895
fwrite(STDERR, 'ERROR: ' . $e->getMessage() . PHP_EOL);
8996
exit(1);

scripts/Phalcon/Builder/Model.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace Phalcon\Builder;
2323

24+
use PDOException;
2425
use Phalcon\Text;
2526
use Phalcon\Utils;
2627
use ReflectionClass;
@@ -34,8 +35,10 @@
3435
use Phalcon\Exception\InvalidArgumentException;
3536
use Phalcon\Options\OptionsAware as ModelOption;
3637
use Phalcon\Exception\InvalidParameterException;
38+
use Phalcon\Exception\Db\PDODriverNotFoundException;
3739
use Phalcon\Validation\Validator\Email as EmailValidator;
3840

41+
3942
/**
4043
* ModelBuilderComponent
4144
*
@@ -191,9 +194,19 @@ public function build()
191194

192195
$adapterName = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;
193196
unset($configArray['adapter']);
194-
/** @var Pdo $db */
195-
$db = new $adapterName($configArray);
196-
197+
try {
198+
/** @var Pdo $db */
199+
$db = new $adapterName($configArray);
200+
} catch(PDOException $e){
201+
switch($e->getMessage())
202+
{
203+
case 'could not find driver':
204+
throw new PDODriverNotFoundException("PDO could not find driver $adapter", $adapter);
205+
break;
206+
default:
207+
throw new PDOException($e->getMessage());
208+
}
209+
}
197210
$initialize = [];
198211

199212
if ($this->shouldInitSchema()) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
+------------------------------------------------------------------------+
5+
| Phalcon Developer Tools |
6+
+------------------------------------------------------------------------+
7+
| Copyright (c) 2011-present Phalcon Team (https://www.phalconphp.com) |
8+
+------------------------------------------------------------------------+
9+
| This source file is subject to the New BSD License that is bundled |
10+
| with this package in the file LICENSE.txt. |
11+
| |
12+
| If you did not receive a copy of the license and are unable to |
13+
| obtain it through the world-wide-web, please send an email |
14+
| to [email protected] so we can send you a copy immediately. |
15+
+------------------------------------------------------------------------+
16+
| Authors: Kevin Yarmak <[email protected]> |
17+
+------------------------------------------------------------------------+
18+
*/
19+
20+
namespace Phalcon\Exception\Db;
21+
22+
use PDOException;
23+
use Phalcon\Script\Color;
24+
use PDO;
25+
26+
class PDODriverNotFoundException extends PDOException
27+
{
28+
protected $adapter = '';
29+
30+
public function __construct($message, $adapter = '')
31+
{
32+
parent::__construct($message);
33+
$this->adapter = $adapter;
34+
}
35+
36+
public function getAdapter()
37+
{
38+
return $this->adapter;
39+
}
40+
41+
public function writeNicelyFormattedErrorOutput()
42+
{
43+
fwrite(STDERR, Color::error($this->getMessage()) . PHP_EOL);
44+
45+
if (!extension_loaded('PDO')) {
46+
fwrite(STDERR, Color::error('PDO extension is not loaded') . PHP_EOL);
47+
} else {
48+
$loadedDrivers = PDO::getAvailableDrivers();
49+
fwrite(STDERR, 'PDO Drivers loaded:' . PHP_EOL);
50+
fwrite(STDERR, print_r($loadedDrivers, true). PHP_EOL);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)