Skip to content

Commit 2e70dd8

Browse files
authored
Merge pull request #8913 from kenjis/fix-postgre-error-msg
fix: [Postgres] show missing error message
2 parents d987732 + 8b3e4e3 commit 2e70dd8

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

system/Database/BaseConnection.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,12 @@ public function initialize()
420420
// Connect to the database and set the connection ID
421421
$this->connID = $this->connect($this->pConnect);
422422
} catch (Throwable $e) {
423-
$connectionErrors[] = sprintf('Main connection [%s]: %s', $this->DBDriver, $e->getMessage());
423+
$this->connID = false;
424+
$connectionErrors[] = sprintf(
425+
'Main connection [%s]: %s',
426+
$this->DBDriver,
427+
$e->getMessage()
428+
);
424429
log_message('error', 'Error connecting to the database: ' . $e);
425430
}
426431

@@ -441,7 +446,12 @@ public function initialize()
441446
// Try to connect
442447
$this->connID = $this->connect($this->pConnect);
443448
} catch (Throwable $e) {
444-
$connectionErrors[] = sprintf('Failover #%d [%s]: %s', ++$index, $this->DBDriver, $e->getMessage());
449+
$connectionErrors[] = sprintf(
450+
'Failover #%d [%s]: %s',
451+
++$index,
452+
$this->DBDriver,
453+
$e->getMessage()
454+
);
445455
log_message('error', 'Error connecting to the database: ' . $e);
446456
}
447457

system/Database/Postgre/Connection.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,24 @@ public function connect(bool $persistent = false)
7878
$this->connID = $persistent === true ? pg_pconnect($this->DSN) : pg_connect($this->DSN);
7979

8080
if ($this->connID !== false) {
81-
if ($persistent === true && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD && pg_ping($this->connID) === false
81+
if (
82+
$persistent === true
83+
&& pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD
84+
&& pg_ping($this->connID) === false
8285
) {
83-
return false;
86+
$error = pg_last_error($this->connID);
87+
88+
throw new DatabaseException($error);
8489
}
8590

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

9095
if ($this->setClientEncoding($this->charset) === false) {
91-
return false;
96+
$error = pg_last_error($this->connID);
97+
98+
throw new DatabaseException($error);
9299
}
93100
}
94101

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live\Postgre;
15+
16+
use CodeIgniter\Database\Exceptions\DatabaseException;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use Config\Database;
19+
use PHPUnit\Framework\Attributes\Group;
20+
21+
/**
22+
* @internal
23+
*/
24+
#[Group('DatabaseLive')]
25+
final class ConnectTest extends CIUnitTestCase
26+
{
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->db = Database::connect($this->DBGroup);
32+
33+
if ($this->db->DBDriver !== 'Postgre') {
34+
$this->markTestSkipped('This test is only for Postgre.');
35+
}
36+
}
37+
38+
public function testShowErrorMessageWhenSettingInvalidCharset(): void
39+
{
40+
$this->expectException(DatabaseException::class);
41+
$this->expectExceptionMessage(
42+
'Unable to connect to the database.
43+
Main connection [Postgre]: ERROR: invalid value for parameter "client_encoding": "utf8mb4"'
44+
);
45+
46+
$config = config('Database');
47+
$group = $config->tests;
48+
// Sets invalid charset.
49+
$group['charset'] = 'utf8mb4';
50+
$db = Database::connect($group);
51+
52+
// Actually connect to DB.
53+
$db->initialize();
54+
}
55+
}

0 commit comments

Comments
 (0)