Skip to content

Commit

Permalink
Make connection exceptions more helpful.
Browse files Browse the repository at this point in the history
Fixes #3204
  • Loading branch information
markstory committed Sep 13, 2012
1 parent 99a9cc9 commit fb93607
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -149,7 +149,10 @@ public function connect() {
);
$this->connected = true;
} catch (PDOException $e) {
throw new MissingConnectionException(array('class' => $e->getMessage()));
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}

$this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");
Expand Down
5 changes: 4 additions & 1 deletion lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -121,7 +121,10 @@ public function connect() {
$this->_execute('SET search_path TO ' . $config['schema']);
}
} catch (PDOException $e) {
throw new MissingConnectionException(array('class' => $e->getMessage()));
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}

return $this->connected;
Expand Down
5 changes: 4 additions & 1 deletion lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -113,7 +113,10 @@ public function connect() {
$this->_connection = new PDO('sqlite:' . $config['database'], null, null, $flags);
$this->connected = true;
} catch(PDOException $e) {
throw new MissingConnectionException(array('class' => $e->getMessage()));
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}
return $this->connected;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -129,7 +129,10 @@ public function connect() {
);
$this->connected = true;
} catch (PDOException $e) {
throw new MissingConnectionException(array('class' => $e->getMessage()));
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}

return $this->connected;
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -253,6 +253,7 @@ public function __construct($config = null, $autoConnect = true) {
if (!$this->enabled()) {
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => __d('cake_dev', 'Selected driver is not enabled'),
'enabled' => false
));
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/Case/Error/ExceptionRendererTest.php
Expand Up @@ -552,18 +552,18 @@ public static function testProvider() {
500
),
array(
new MissingConnectionException(array('class' => 'Article')),
new MissingConnectionException(array('class' => 'Mysql')),
array(
'/<h2>Missing Database Connection<\/h2>/',
'/Article requires a database connection/'
'/A Database connection using "Mysql" was missing or unable to connect./',
),
500
),
array(
new MissingConnectionException(array('class' => 'Mysql', 'enabled' => false)),
array(
'/<h2>Missing Database Connection<\/h2>/',
'/Mysql requires a database connection/',
'/A Database connection using "Mysql" was missing or unable to connect./',
'/Mysql driver is NOT enabled/'
),
500
Expand Down
8 changes: 7 additions & 1 deletion lib/Cake/View/Errors/missing_connection.ctp
Expand Up @@ -19,7 +19,13 @@
<h2><?php echo __d('cake_dev', 'Missing Database Connection'); ?></h2>
<p class="error">
<strong><?php echo __d('cake_dev', 'Error'); ?>: </strong>
<?php echo __d('cake_dev', '%s requires a database connection', $class); ?>
<?php echo __d('cake_dev', 'A Database connection using "%s" was missing or unable to connect. ', $class); ?>
<br />
<?php
if (isset($message)):
echo __d('cake_dev', 'The database server returned this error: %s', $message);
endif;
?>
</p>
<?php if (!$enabled) : ?>
<p class="error">
Expand Down

0 comments on commit fb93607

Please sign in to comment.