diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 5e3344d..edfbc70 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -1430,7 +1430,7 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) { protected static function getSQLState(\Exception $e) { // The PDOException code is not always reliable, try to see whether the // message has something usable. - if (!$e->getCode() && preg_match('/^SQLSTATE\[(\w{5})\]/', $e->getMessage(), $matches)) { + if (preg_match('/^SQLSTATE\[(\w{5})\]/', $e->getMessage(), $matches)) { return $matches[1]; } else { diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index 249c2b2..e13b1e7 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -28,12 +28,12 @@ class Connection extends DatabaseConnection { const DATABASE_NOT_FOUND = 7; /** - * Error code for "Access denied" error. + * Error code for "Connection failure" errors. * * Technically this is an internal error code that will only be shown in the * PDOException message. It will need to get extracted. */ - const ACCESS_DENIED = '08006'; + const CONNECTION_FAILURE = '08006'; /** * The list of PostgreSQL reserved key words. @@ -127,11 +127,13 @@ public static function open(array &$connection_options = array()) { $pdo = new \PDO($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']); } catch (\PDOException $e) { - if ($e->getCode() == static::DATABASE_NOT_FOUND) { - throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e); - } - if (static::getSQLState($e) == self::ACCESS_DENIED) { - throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e); + if (static::getSQLState($e) == static::CONNECTION_FAILURE) { + if (strpos($e->getMessage(), 'password authentication failed for user') !== FALSE) { + throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e); + } + elseif (strpos($e->getMessage(), 'database') !== FALSE && strpos($e->getMessage(), 'does not exist') !== FALSE) { + throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e); + } } throw $e; } diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php index 95ff527..7c85eaf 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php @@ -65,8 +65,8 @@ protected function connect() { $this->pass('Drupal can CONNECT to the database ok.'); } catch (\Exception $e) { - // Attempt to create the database if it is not found. - if ($e->getCode() == Connection::DATABASE_NOT_FOUND) { + // Attempt to create the database if it is not found. + if ($e instanceof DatabaseNotFoundException) { // Remove the database string from connection info. $connection_info = Database::getConnectionInfo(); $database = $connection_info['default']['database'];