Use error suppression instead of an error handler in MySQLi Connection

parent a05f4493
...@@ -17,8 +17,6 @@ use function mysqli_errno; ...@@ -17,8 +17,6 @@ use function mysqli_errno;
use function mysqli_error; use function mysqli_error;
use function mysqli_init; use function mysqli_init;
use function mysqli_options; use function mysqli_options;
use function restore_error_handler;
use function set_error_handler;
use function sprintf; use function sprintf;
use function stripos; use function stripos;
...@@ -70,16 +68,12 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection ...@@ -70,16 +68,12 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection
$this->setSecureConnection($params); $this->setSecureConnection($params);
$this->setDriverOptions($driverOptions); $this->setDriverOptions($driverOptions);
set_error_handler(static function (): bool { if (! @$this->conn->real_connect($host, $username, $password, $dbname, $port, $socket, $flags)) {
return true; throw new MysqliException(
}); $this->conn->connect_error,
$this->conn->sqlstate ?? 'HY000',
try { $this->conn->connect_errno
if (! $this->conn->real_connect($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();
} }
if (! isset($params['charset'])) { if (! isset($params['charset'])) {
......
...@@ -4,14 +4,11 @@ namespace Doctrine\DBAL\Tests\Driver\Mysqli; ...@@ -4,14 +4,11 @@ namespace Doctrine\DBAL\Tests\Driver\Mysqli;
use Doctrine\DBAL\Driver\Mysqli\HostRequired; use Doctrine\DBAL\Driver\Mysqli\HostRequired;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection; use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\FunctionalTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use function extension_loaded; use function extension_loaded;
use function restore_error_handler;
use function set_error_handler;
class MysqliConnectionTest extends FunctionalTestCase class MysqliConnectionTest extends FunctionalTestCase
{ {
...@@ -44,26 +41,6 @@ class MysqliConnectionTest extends FunctionalTestCase ...@@ -44,26 +41,6 @@ class MysqliConnectionTest extends FunctionalTestCase
self::assertFalse($this->connectionMock->requiresQueryForServerVersion()); self::assertFalse($this->connectionMock->requiresQueryForServerVersion());
} }
public function testRestoresErrorHandlerOnException(): void
{
$handler = static function (): bool {
self::fail('Never expected this to be called');
};
$defaultHandler = 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($defaultHandler), 'Restoring error handler failed.');
restore_error_handler();
restore_error_handler();
}
public function testHostnameIsRequiredForPersistentConnection(): void public function testHostnameIsRequiredForPersistentConnection(): void
{ {
$this->expectException(HostRequired::class); $this->expectException(HostRequired::class);
......
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