diff --git a/core/DB/PgSQL.class.php b/core/DB/PgSQL.class.php index 7a450f7c2b..cacec8acad 100644 --- a/core/DB/PgSQL.class.php +++ b/core/DB/PgSQL.class.php @@ -37,15 +37,18 @@ public function connect() .($this->basename ? " dbname={$this->basename}" : null) .($this->port ? " port={$this->port}" : null); - if ($this->persistent) - $this->link = pg_pconnect($conn); - else - $this->link = pg_connect($conn); - - if (!$this->link) + try { + if ($this->persistent) + $this->link = pg_pconnect($conn); + else + $this->link = pg_connect($conn); + } catch (Exception $e) { throw new DatabaseException( - 'can not connect to PostgreSQL server: '.pg_errormessage() + 'can not connect to PostgreSQL server: '.$e->getMessage(), + $e->getCode(), + $e ); + } if ($this->encoding) $this->setDbEncoding(); diff --git a/doc/ChangeLog b/doc/ChangeLog index 622da55aed..31a0324a8c 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2012-06-29 N. Konstantinov + + * core/DB/PgSQL.class.php + test/core/DbConnectionTest.class.php: + + throw DatabaseException instead of BaseException in case of connection's failure + 2012-06-27 Alexey S. Denisov * main/Base/AbstractProtoClass.class.php diff --git a/test/core/DbConnectionTest.class.php b/test/core/DbConnectionTest.class.php new file mode 100644 index 0000000000..babe3ac7c3 --- /dev/null +++ b/test/core/DbConnectionTest.class.php @@ -0,0 +1,30 @@ + + addLink( + 'badLink', + DB::spawn('PinbedPgSQL', 'postgres', '', 'localhost', 'wrongDatabase') + ); + } + + public function testPostgresql() + { + try { + $link = DBPool::me()->getLink('badLink'); + $this->fail('Unreachable code'); + } catch(Exception $e) { + $this->assertInstanceOf('DatabaseException', $e); + } + } + + public function tearDown() + { + DBPool::me()->dropLink('badLink'); + } + } + +?> \ No newline at end of file