Skip to content

Commit

Permalink
Map mysqlnd Packets out of order error as ConnectionException (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Nov 3, 2021
1 parent ce2fe5e commit e4c9e5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Driver/MySQL/MySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function mapException(\Throwable $exception, string $query): Statement
strpos($message, 'server has gone away') !== false
|| strpos($message, 'broken pipe') !== false
|| strpos($message, 'connection') !== false
|| strpos($message, 'packets out of order') !== false
|| ((int)$exception->getCode() > 2000 && (int)$exception->getCode() < 2100)
) {
return new StatementException\ConnectionException($exception, $query);
Expand Down
22 changes: 22 additions & 0 deletions tests/Database/Driver/MySQL/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,33 @@

namespace Cycle\Database\Tests\Driver\MySQL;

use Spiral\Database\Exception\StatementException\ConnectionException;

/**
* @group driver
* @group driver-mysql
*/
class ExceptionsTest extends \Cycle\Database\Tests\ExceptionsTest
{
public const DRIVER = 'mysql';

public function testPacketsOutOfOrderConsideredAsConnectionException(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Expecting PHP version >=7.4. Skipped due to ' . PHP_VERSION);
}

// Prepare connection to generate "Packets out of order. Expected 1 received 0. Packet size=145"
// at the next query response
$this->database->query("SET SESSION wait_timeout=1")->fetch();
sleep(1);

try {
$result = $this->database->query('SELECT version() AS version')->fetchAll();
$this->assertNotEmpty($result[0]['version'] ?? '', 'Expected result from second query');
} catch (\RuntimeException $e) {
$this->assertInstanceOf(ConnectionException::class, $e);
return;
}
}
}

0 comments on commit e4c9e5f

Please sign in to comment.