Skip to content

Commit 19f336d

Browse files
committed
fixed regression into message exception handling
1 parent 705830f commit 19f336d

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/Bolt/BoltResult.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
namespace Laudis\Neo4j\Bolt;
1515

1616
use function array_splice;
17+
use Bolt\error\MessageException;
1718
use function count;
1819
use Generator;
20+
use function in_array;
1921
use Iterator;
2022
use Laudis\Neo4j\Contracts\FormatterInterface;
23+
use Laudis\Neo4j\Exception\Neo4jException;
2124

2225
/**
2326
* @psalm-import-type BoltCypherStats from FormatterInterface
@@ -99,7 +102,11 @@ public function consume(): array
99102

100103
private function fetchResults(): void
101104
{
102-
$meta = $this->connection->pull($this->qid, $this->fetchSize);
105+
try {
106+
$meta = $this->connection->pull($this->qid, $this->fetchSize);
107+
} catch (MessageException $e) {
108+
$this->handleMessageException($e);
109+
}
103110

104111
/** @var list<list> $rows */
105112
$rows = array_splice($meta, 0, count($meta) - 1);
@@ -148,6 +155,25 @@ public function __destruct()
148155

149156
public function discard(): void
150157
{
151-
$this->connection->discard($this->qid === -1 ? null : $this->qid);
158+
try {
159+
$this->connection->discard($this->qid === -1 ? null : $this->qid);
160+
} catch (MessageException $e) {
161+
$this->handleMessageException($e);
162+
}
163+
}
164+
165+
/**
166+
* @throws Neo4jException
167+
*
168+
* @return never
169+
*/
170+
private function handleMessageException(MessageException $e): void
171+
{
172+
$exception = Neo4jException::fromMessageException($e);
173+
if (!($exception->getClassification() === 'ClientError' && $exception->getCategory() === 'Request')) {
174+
$this->connection->reset();
175+
}
176+
177+
throw $exception;
152178
}
153179
}

tests/Integration/ComplexQueryTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Laudis\Neo4j\Tests\Integration;
1515

16-
use Bolt\error\MessageException;
1716
use Generator;
1817
use function getenv;
1918
use InvalidArgumentException;
@@ -30,6 +29,7 @@
3029
use Laudis\Neo4j\ParameterHelper;
3130
use Laudis\Neo4j\Types\CypherMap;
3231
use Laudis\Neo4j\Types\Node;
32+
use const PHP_EOL;
3333
use function str_starts_with;
3434
use Throwable;
3535

@@ -479,14 +479,23 @@ public function testConstraintHandling(string $alias): void
479479
{
480480
$session = $this->getClient()->getDriver($alias)->createSession();
481481

482+
$session->run('MATCH (x) DETACH DELETE x');
482483
$session->run("CREATE (test:Test{id: '123'})");
484+
485+
// We cannot use IF EXISTS on version 4.0
486+
try {
487+
$session->run('DROP CONSTRAINT ON (test:Test) ASSERT test.id IS UNIQUE');
488+
} catch (Throwable $e) {
489+
echo $e->getMessage().PHP_EOL;
490+
}
491+
483492
try {
484493
$session->run('CREATE CONSTRAINT ON (test:Test) ASSERT test.id IS UNIQUE');
485494
} catch (Throwable $e) {
486-
// We cannot use IF EXISTS on version 4.0
487-
$session->run('DROP CONSTRAINT ON (test:Test) ASSERT test.id IS UNIQUE');
495+
echo $e->getMessage().PHP_EOL;
488496
}
489-
$this->expectException(MessageException::class);
497+
498+
$this->expectException(Neo4jException::class);
490499
$session->run("CREATE (test:Test {id: '123'}) RETURN test");
491500
}
492501

0 commit comments

Comments
 (0)