Commit 663cb650 authored by Piotr Różański's avatar Piotr Różański

Better exception handling in mysqli connect

parent fbed76eb
......@@ -67,15 +67,14 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
$this->setDriverOptions($driverOptions);
set_error_handler(function () {});
if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
try {
if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
throw new MysqliException($this->_conn->connect_error, $this->_conn->sqlstate ?? 'HY000', $this->_conn->connect_errno);
}
} finally {
restore_error_handler();
throw new MysqliException($this->_conn->connect_error, @$this->_conn->sqlstate ?: 'HY000', $this->_conn->connect_errno);
}
restore_error_handler();
if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']);
}
......
......@@ -28,11 +28,33 @@ class MysqliConnectionTest extends DbalTestCase
->getMockForAbstractClass();
}
protected function tearDown()
{
if ($this->getName() == "testRestoresErrorHandlerOnException") {
restore_error_handler();
}
}
public function testDoesNotRequireQueryForServerVersion()
{
$this->assertFalse($this->connectionMock->requiresQueryForServerVersion());
}
public function testRestoresErrorHandlerOnException()
{
$handler = function () { self::fail('Never expected this to be called'); };
$default_handler = set_error_handler($handler);
try {
new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass');
self::fail('An exception was supposed to be raised');
} catch (MysqliException $e) {
self::assertSame('Network is unreachable', $e->getMessage());
}
self::assertSame($handler, set_error_handler($default_handler), 'Restoring error handler failed.');
}
/**
* @dataProvider secureMissingParamsProvider
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment