1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Handle PostgreSQL connection errors

This commit is contained in:
J. King 2018-12-12 11:15:07 -05:00
parent 0f48ce6f37
commit 28f803dd28
4 changed files with 29 additions and 6 deletions

View file

@ -26,6 +26,7 @@ abstract class AbstractException extends \Exception {
"Db/Exception.fileUnwritable" => 10205, "Db/Exception.fileUnwritable" => 10205,
"Db/Exception.fileUncreatable" => 10206, "Db/Exception.fileUncreatable" => 10206,
"Db/Exception.fileCorrupt" => 10207, "Db/Exception.fileCorrupt" => 10207,
"Db/Exception.connectionFailure" => 10208,
"Db/Exception.updateTooNew" => 10211, "Db/Exception.updateTooNew" => 10211,
"Db/Exception.updateManual" => 10212, "Db/Exception.updateManual" => 10212,
"Db/Exception.updateManualOnly" => 10213, "Db/Exception.updateManualOnly" => 10213,

View file

@ -22,10 +22,22 @@ class PDODriver extends Driver {
protected function makeConnection(string $user, string $pass, string $db, string $host, int $port, string $service) { protected function makeConnection(string $user, string $pass, string $db, string $host, int $port, string $service) {
$dsn = $this->makeconnectionString(true, $user, $pass, $db, $host, $port, $service); $dsn = $this->makeconnectionString(true, $user, $pass, $db, $host, $port, $service);
try {
$this->db = new \PDO("pgsql:$dsn", $user, $pass, [ $this->db = new \PDO("pgsql:$dsn", $user, $pass, [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_PERSISTENT => true, \PDO::ATTR_PERSISTENT => true,
]); ]);
} catch (\PDOException $e) {
if ($e->getCode() == 7) {
switch (substr($e->getMessage(), 9, 5)) {
case "08006":
throw new Exception("connectionFailure", ["PostgreSQL", substr($e->getMessage(), 28)]);
default:
throw $e; // @codeCoverageIgnore
}
}
throw $e; // @codeCoverageIgnore
}
} }
public function __destruct() { public function __destruct() {

View file

@ -122,6 +122,7 @@ return [
'Exception.JKingWeb/Arsse/Db/Exception.fileUnusable' => 'Insufficient permissions to open database file "{0}" for reading or writing', 'Exception.JKingWeb/Arsse/Db/Exception.fileUnusable' => 'Insufficient permissions to open database file "{0}" for reading or writing',
'Exception.JKingWeb/Arsse/Db/Exception.fileUncreatable' => 'Insufficient permissions to create new database file "{0}"', 'Exception.JKingWeb/Arsse/Db/Exception.fileUncreatable' => 'Insufficient permissions to create new database file "{0}"',
'Exception.JKingWeb/Arsse/Db/Exception.fileCorrupt' => 'Database file "{0}" is corrupt or not a valid database', 'Exception.JKingWeb/Arsse/Db/Exception.fileCorrupt' => 'Database file "{0}" is corrupt or not a valid database',
'Exception.JKingWeb/Arsse/Db/Exception.connectionFailure' => 'Could not connect to {0} database: {1}',
'Exception.JKingWeb/Arsse/Db/Exception.paramTypeInvalid' => 'Prepared statement parameter type "{0}" is invalid', 'Exception.JKingWeb/Arsse/Db/Exception.paramTypeInvalid' => 'Prepared statement parameter type "{0}" is invalid',
'Exception.JKingWeb/Arsse/Db/Exception.paramTypeUnknown' => 'Prepared statement parameter type "{0}" is valid, but not implemented', 'Exception.JKingWeb/Arsse/Db/Exception.paramTypeUnknown' => 'Prepared statement parameter type "{0}" is valid, but not implemented',
'Exception.JKingWeb/Arsse/Db/Exception.paramTypeMissing' => 'Prepared statement parameter type for parameter #{0} was not specified', 'Exception.JKingWeb/Arsse/Db/Exception.paramTypeMissing' => 'Prepared statement parameter type for parameter #{0} was not specified',

View file

@ -7,11 +7,11 @@ declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL; namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL;
use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\PostgreSQL\Driver; use JKingWeb\Arsse\Db\PostgreSQL\PDODriver as Driver;
/** /**
* @group slow * @group slow
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Driver<extended> */ * @covers \JKingWeb\Arsse\Db\PostgreSQL\PDODriver<extended> */
class TestCreation extends \JKingWeb\Arsse\Test\AbstractTest { class TestCreation extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideConnectionStrings */ /** @dataProvider provideConnectionStrings */
public function testGenerateConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port, string $service, string $exp) { public function testGenerateConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port, string $service, string $exp) {
@ -55,4 +55,13 @@ class TestCreation extends \JKingWeb\Arsse\Test\AbstractTest {
[true, "T'Pau of Vulcan", "superman", "datumbase", "somehost", 2112, "arsse", "service='arsse'"], [true, "T'Pau of Vulcan", "superman", "datumbase", "somehost", 2112, "arsse", "service='arsse'"],
]; ];
} }
public function testFailToConnect() {
// PDO dies not distinguish between different connection failure modes
self::setConf([
'dbPostgreSQLPass' => (string) rand(),
]);
$this->assertException("connectionFailure", "Db");
new Driver;
}
} }