Skip to content

Commit 8558407

Browse files
committed
added neo4j error codes to exceptions
1 parent 53eb761 commit 8558407

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/Bolt/BoltUnmanagedTransaction.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Bolt\error\MessageException;
1818
use Ds\Vector;
1919
use Exception;
20+
use Laudis\Neo4j\Common\TransactionHelper;
2021
use Laudis\Neo4j\Contracts\FormatterInterface;
2122
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
2223
use Laudis\Neo4j\Databags\Neo4jError;
@@ -62,7 +63,8 @@ public function commit(iterable $statements = []): CypherList
6263
$this->bolt->commit();
6364
$this->finished = true;
6465
} catch (Exception $e) {
65-
throw new Neo4jException(new Vector([new Neo4jError('', $e->getMessage())]), $e);
66+
$code = TransactionHelper::extractCode($e);
67+
throw new Neo4jException(new Vector([new Neo4jError($code ?? '', $e->getMessage())]), $e);
6668
}
6769

6870
return $tbr;
@@ -78,7 +80,8 @@ public function rollback(): void
7880
$this->bolt->rollback();
7981
$this->finished = true;
8082
} catch (Exception $e) {
81-
throw new Neo4jException(new Vector([new Neo4jError('', $e->getMessage())]), $e);
83+
$code = TransactionHelper::extractCode($e) ?? '';
84+
throw new Neo4jException(new Vector([new Neo4jError($code, $e->getMessage())]), $e);
8285
}
8386
}
8487

@@ -115,7 +118,8 @@ public function runStatements(iterable $statements): CypherList
115118
$results = $this->bolt->pullAll();
116119
} catch (Throwable $e) {
117120
if ($e instanceof MessageException) {
118-
throw new Neo4jException(new Vector([new Neo4jError('', $e->getMessage())]), $e);
121+
$code = TransactionHelper::extractCode($e) ?? '';
122+
throw new Neo4jException(new Vector([new Neo4jError($code, $e->getMessage())]), $e);
119123
}
120124
throw $e;
121125
}

src/Bolt/Session.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ private function startTransaction(TransactionConfiguration $config, SessionConfi
165165
if ($e instanceof Neo4jException) {
166166
throw $e;
167167
}
168-
throw new Neo4jException(new Vector([new Neo4jError('', $e->getMessage())]), $e);
168+
$code = TransactionHelper::extractCode($e) ?? '';
169+
throw new Neo4jException(new Vector([new Neo4jError($code, $e->getMessage())]), $e);
169170
}
170171

171172
return new BoltUnmanagedTransaction($this->config->getDatabase(), $this->formatter, $bolt);

src/Common/TransactionHelper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use Laudis\Neo4j\Databags\TransactionConfiguration;
2222
use Laudis\Neo4j\Exception\Neo4jException;
2323
use function microtime;
24+
use function preg_match;
25+
use const PREG_OFFSET_CAPTURE;
26+
use Throwable;
2427

2528
final class TransactionHelper
2629
{
@@ -73,4 +76,19 @@ public static function enableSsl(string $host, string $sslConfig, StreamSocket $
7376
$sock->setSslContextOptions($options);
7477
}
7578
}
79+
80+
public static function extractCode(Throwable $throwable): ?string
81+
{
82+
$message = $throwable->getMessage();
83+
$matches = [];
84+
preg_match('/\(Neo\.([\w]+\.?)+\)/', $message, $matches, PREG_OFFSET_CAPTURE);
85+
/** @var list<array{0: string, 1: int}> $matches */
86+
if (isset($matches[0])) {
87+
$code = $matches[0][0];
88+
89+
return str_replace(['(', ')'], '', $code);
90+
}
91+
92+
return null;
93+
}
7694
}

src/Exception/Neo4jException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ public function getErrors(): Vector
4242
{
4343
return $this->errors;
4444
}
45+
46+
public function getNeo4jCode(): string
47+
{
48+
return $this->errors->first()->getCode();
49+
}
4550
}

tests/Integration/TransactionIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testInvalidRun(string $alias): void
6464
$transaction->run('MERGE (x:Tes0342hdm21.())', ['test' => 'a', 'otherTest' => 'b']);
6565
} catch (Neo4jException $e) {
6666
$exception = true;
67+
self::assertEquals('Neo.ClientError.Statement.SyntaxError', $e->getNeo4jCode());
6768
}
6869
self::assertTrue($exception);
6970
}

0 commit comments

Comments
 (0)