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

Commit

Permalink
fix issue when a neo4j exception is thrown during a tx and not report…
Browse files Browse the repository at this point in the history
…ed back to user
  • Loading branch information
ikwattro committed Sep 4, 2016
1 parent 0b11e78 commit af8f014
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/HttpDriver/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,15 @@ public function pushToTransaction($transactionId, array $statementsStack)

try {
$response = $this->httpClient->send($request);
$data = json_decode((string) $response->getBody(), true);
if (!empty($data['errors'])) {
$msg = sprintf('Neo4j Exception with code "%s" and message "%s"', $data['errors'][0]['code'], $data['errors'][0]['message']);
$exception = new Neo4jException($msg);
$exception->setNeo4jStatusCode($data['errors'][0]['code']);

throw $exception;

}
return $this->responseFormatter->format(json_decode($response->getBody(), true), $statementsStack);
} catch (RequestException $e) {
if ($e->hasResponse()) {
Expand Down Expand Up @@ -283,7 +291,15 @@ public function commitTransaction($transactionId)
{
$request = new Request('POST', sprintf('%s/db/data/transaction/%d/commit', $this->uri, $transactionId));
try {
$this->httpClient->send($request);
$response = $this->httpClient->send($request);
$data = json_decode((string) $response->getBody(), true);
if (!empty($data['errors'])) {
$msg = sprintf('Neo4j Exception with code "%s" and message "%s"', $data['errors'][0]['code'], $data['errors'][0]['message']);
$exception = new Neo4jException($msg);
$exception->setNeo4jStatusCode($data['errors'][0]['code']);
throw $exception;

}
} catch (RequestException $e) {
if ($e->hasResponse()) {
$body = json_decode($e->getResponse()->getBody(), true);
Expand Down
14 changes: 13 additions & 1 deletion src/HttpDriver/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GraphAware\Common\Transaction\TransactionInterface;
use GraphAware\Neo4j\Client\Exception\Neo4jException;
use GraphAware\Common\Cypher\Statement;
use GraphAware\Neo4j\Client\Exception\Neo4jExceptionInterface;

class Transaction implements TransactionInterface
{
Expand Down Expand Up @@ -41,6 +42,8 @@ class Transaction implements TransactionInterface

protected $expires;

protected $pending = [];

/**
* @param Session $session
*/
Expand Down Expand Up @@ -179,7 +182,15 @@ public function success()
{
$this->assertNotClosed();
$this->assertStarted();
$this->session->commitTransaction($this->transactionId);
try {
$this->session->commitTransaction($this->transactionId);
} catch (Neo4jException $e) {
if ($e->effect() === Neo4jExceptionInterface::EFFECT_ROLLBACK) {
$this->state = self::ROLLED_BACK;
}

throw $e;
}
$this->state = self::COMMITED;
$this->closed = true;
$this->session->transaction = null;
Expand All @@ -194,6 +205,7 @@ private function assertStarted()
{
if ($this->state !== self::OPENED) {
throw new \RuntimeException('This transaction has not been started');
//$this->begin();
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Issues/ReportedIssuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace GraphAware\Neo4j\Client\Tests\Issues;

use GraphAware\Neo4j\Client\Tests\Integration\IntegrationTestCase;
use GraphAware\Neo4j\Client\Exception\Neo4jException;
use Symfony\Component\Yaml\Exception\RuntimeException;

class ReportedIssuesTest extends IntegrationTestCase
{
/**
*
* @group issue-so-2
*/
public function testTryingToDeleteNodeWithRelsInTransactionShouldFail()
{
$this->emptyDb();
$this->createNodeWithRels();
$tx = $this->client->transaction();
$tx->push('MATCH (n:Node) DELETE n');
$this->setExpectedException(Neo4jException::class);
$tx->commit();
}

/**
*
* @group issue-so-3
*/
public function testTryingToDeleteNodeWithRelsInTransactionShouldFailAndTxBeRolledBack()
{
$this->emptyDb();
$this->createNodeWithRels();
$tx = $this->client->transaction();
$tx->push('MATCH (n:Node) DELETE n');
try {
$tx->commit();
// it should fail
throw new RuntimeException();
} catch (Neo4jException $e) {
$this->assertTrue($tx->isRolledBack());
}
}

private function createNodeWithRels()
{
$this->client->run('CREATE (n:Node)-[:REL]->(:OtherNode)');
}
}

0 comments on commit af8f014

Please sign in to comment.