Skip to content

Commit

Permalink
Merge pull request #8913 from kenjis/fix-postgre-error-msg
Browse files Browse the repository at this point in the history
fix: [Postgres] show missing error message
  • Loading branch information
kenjis authored May 31, 2024
2 parents d987732 + 8b3e4e3 commit 2e70dd8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
14 changes: 12 additions & 2 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,12 @@ public function initialize()
// Connect to the database and set the connection ID
$this->connID = $this->connect($this->pConnect);
} catch (Throwable $e) {
$connectionErrors[] = sprintf('Main connection [%s]: %s', $this->DBDriver, $e->getMessage());
$this->connID = false;
$connectionErrors[] = sprintf(
'Main connection [%s]: %s',
$this->DBDriver,
$e->getMessage()
);
log_message('error', 'Error connecting to the database: ' . $e);
}

Expand All @@ -441,7 +446,12 @@ public function initialize()
// Try to connect
$this->connID = $this->connect($this->pConnect);
} catch (Throwable $e) {
$connectionErrors[] = sprintf('Failover #%d [%s]: %s', ++$index, $this->DBDriver, $e->getMessage());
$connectionErrors[] = sprintf(
'Failover #%d [%s]: %s',
++$index,
$this->DBDriver,
$e->getMessage()
);
log_message('error', 'Error connecting to the database: ' . $e);
}

Expand Down
13 changes: 10 additions & 3 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,24 @@ public function connect(bool $persistent = false)
$this->connID = $persistent === true ? pg_pconnect($this->DSN) : pg_connect($this->DSN);

if ($this->connID !== false) {
if ($persistent === true && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD && pg_ping($this->connID) === false
if (
$persistent === true
&& pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD
&& pg_ping($this->connID) === false
) {
return false;
$error = pg_last_error($this->connID);

throw new DatabaseException($error);
}

if (! empty($this->schema)) {
$this->simpleQuery("SET search_path TO {$this->schema},public");
}

if ($this->setClientEncoding($this->charset) === false) {
return false;
$error = pg_last_error($this->connID);

throw new DatabaseException($error);
}
}

Expand Down
55 changes: 55 additions & 0 deletions tests/system/Database/Live/Postgre/ConnectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Live\Postgre;

use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('DatabaseLive')]
final class ConnectTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->db = Database::connect($this->DBGroup);

if ($this->db->DBDriver !== 'Postgre') {
$this->markTestSkipped('This test is only for Postgre.');
}
}

public function testShowErrorMessageWhenSettingInvalidCharset(): void
{
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage(
'Unable to connect to the database.
Main connection [Postgre]: ERROR: invalid value for parameter "client_encoding": "utf8mb4"'
);

$config = config('Database');
$group = $config->tests;
// Sets invalid charset.
$group['charset'] = 'utf8mb4';
$db = Database::connect($group);

// Actually connect to DB.
$db->initialize();
}
}

0 comments on commit 2e70dd8

Please sign in to comment.