Commit 0061022d authored by Marco Pivetta's avatar Marco Pivetta

Merge branch 'fix/#2704-better-mysqli-exception-handling'

Close #2704
parents fbed76eb 3ae969f6
......@@ -67,14 +67,13 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
$this->setDriverOptions($driverOptions);
set_error_handler(function () {});
try {
if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
restore_error_handler();
throw new MysqliException($this->_conn->connect_error, @$this->_conn->sqlstate ?: 'HY000', $this->_conn->connect_errno);
throw new MysqliException($this->_conn->connect_error, $this->_conn->sqlstate ?? 'HY000', $this->_conn->connect_errno);
}
} finally {
restore_error_handler();
}
if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']);
......
......@@ -33,6 +33,23 @@ class MysqliConnectionTest extends DbalTestCase
$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.');
restore_error_handler();
restore_error_handler();
}
/**
* @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