Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 8a5c2eb

Browse files
author
Stefan Gehrig
committed
converts MessageFailureExceptions to Neo4jExceptions to have a common interface when running queries
also dispatches failure events
1 parent 546f105 commit 8a5c2eb

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/Transaction/Transaction.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

1212
namespace GraphAware\Neo4j\Client\Transaction;
1313

14+
use GraphAware\Bolt\Exception\MessageFailureException;
1415
use GraphAware\Common\Cypher\Statement;
1516
use GraphAware\Common\Result\Result;
1617
use GraphAware\Common\Transaction\TransactionInterface;
18+
use GraphAware\Neo4j\Client\Event\FailureEvent;
1719
use GraphAware\Neo4j\Client\Event\PostRunEvent;
1820
use GraphAware\Neo4j\Client\Event\PreRunEvent;
21+
use GraphAware\Neo4j\Client\Exception\Neo4jException;
1922
use GraphAware\Neo4j\Client\Neo4jClientEvents;
2023
use GraphAware\Neo4j\Client\Result\ResultCollection;
2124
use GraphAware\Neo4j\Client\StackInterface;
@@ -65,7 +68,9 @@ public function push($statement, array $parameters = [], $tag = null)
6568
* @param array $parameters
6669
* @param null|string $tag
6770
*
68-
* @return \GraphAware\Common\Result\Result
71+
* @throws Neo4jException
72+
*
73+
* @return \GraphAware\Common\Result\Result|null
6974
*/
7075
public function run($statement, array $parameters = [], $tag = null)
7176
{
@@ -74,8 +79,20 @@ public function run($statement, array $parameters = [], $tag = null)
7479
}
7580
$stmt = Statement::create($statement, $parameters, $tag);
7681
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_PRE_RUN, new PreRunEvent([$stmt]));
77-
$result = $this->driverTransaction->run(Statement::create($statement, $parameters, $tag));
78-
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent(ResultCollection::withResult($result)));
82+
try {
83+
$result = $this->driverTransaction->run(Statement::create($statement, $parameters, $tag));
84+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent(ResultCollection::withResult($result)));
85+
} catch (MessageFailureException $e) {
86+
$exception = new Neo4jException($e->getMessage());
87+
$exception->setNeo4jStatusCode($e->getStatusCode());
88+
89+
$event = new FailureEvent($exception);
90+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_ON_FAILURE, $event);
91+
if ($event->shouldThrowException()) {
92+
throw $exception;
93+
}
94+
return null;
95+
}
7996

8097
return $result;
8198
}
@@ -93,7 +110,9 @@ public function pushStack(StackInterface $stack)
93110
/**
94111
* @param StackInterface $stack
95112
*
96-
* @return ResultCollection|Result[]
113+
* @throws Neo4jException
114+
*
115+
* @return ResultCollection|Result[]|null
97116
*/
98117
public function runStack(StackInterface $stack)
99118
{
@@ -108,8 +127,20 @@ public function runStack(StackInterface $stack)
108127
}
109128

110129
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_PRE_RUN, new PreRunEvent($stack->statements()));
111-
$results = $this->driverTransaction->runMultiple($sts);
112-
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent($results));
130+
try {
131+
$results = $this->driverTransaction->runMultiple($sts);
132+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent($results));
133+
} catch (MessageFailureException $e) {
134+
$exception = new Neo4jException($e->getMessage());
135+
$exception->setNeo4jStatusCode($e->getStatusCode());
136+
137+
$event = new FailureEvent($exception);
138+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_ON_FAILURE, $event);
139+
if ($event->shouldThrowException()) {
140+
throw $exception;
141+
}
142+
return null;
143+
}
113144

114145
return $results;
115146
}

0 commit comments

Comments
 (0)