diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 791bf7a..5e3344d 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -1419,6 +1419,26 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) { } /** + * Extracts the SQLSTATE error from the PDOException. + * + * @param \Exception $e + * The exception + * + * @return string + * The five character error code. + */ + 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)) { + return $matches[1]; + } + else { + return $e->getCode(); + } + } + + /** * Prevents the database connection from being serialized. */ public function __sleep() { diff --git a/core/lib/Drupal/Core/Database/DatabaseAccessDeniedException.php b/core/lib/Drupal/Core/Database/DatabaseAccessDeniedException.php index a797fea..85bfea9 100644 --- a/core/lib/Drupal/Core/Database/DatabaseAccessDeniedException.php +++ b/core/lib/Drupal/Core/Database/DatabaseAccessDeniedException.php @@ -3,6 +3,6 @@ namespace Drupal\Core\Database; /** - * Exception thrown if specified database is not found. + * Exception thrown if access credentials fail. */ class DatabaseAccessDeniedException extends \RuntimeException implements DatabaseException {} diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index 042c221..249c2b2 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -30,9 +30,10 @@ class Connection extends DatabaseConnection { /** * Error code for "Access denied" error. * - * Technically the error message is "insufficient_privilege". + * 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 = '42501'; + const ACCESS_DENIED = '08006'; /** * The list of PostgreSQL reserved key words. @@ -129,7 +130,7 @@ public static function open(array &$connection_options = array()) { if ($e->getCode() == static::DATABASE_NOT_FOUND) { throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e); } - if ($e->getCode() == static::ACCESS_DENIED) { + if (static::getSQLState($e) == self::ACCESS_DENIED) { throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e); } throw $e;