Skip to content

Commit

Permalink
Fixed transaction failure bug for HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
technige committed Jul 18, 2020
1 parent a1793a7 commit 65c44d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
23 changes: 17 additions & 6 deletions py2neo/client/http.py
Expand Up @@ -143,8 +143,8 @@ def auto_run(self, graph_name, cypher, parameters=None,
r = self._post(HTTPTransaction.autocommit_uri(graph_name), cypher, parameters)
assert r.status == 200 # TODO: other codes
rs = HTTPResponse.from_json(r.data.decode("utf-8"))
rs.audit()
self.release()
rs.audit()
return HTTPResult(graph_name, rs.result())

def begin(self, graph_name, readonly=False, after=None, metadata=None, timeout=None):
Expand All @@ -163,11 +163,11 @@ def begin(self, graph_name, readonly=False, after=None, metadata=None, timeout=N
if r.status != 201:
raise RuntimeError("Can't begin a new transaction") # TODO: better error
rs = HTTPResponse.from_json(r.data.decode("utf-8"))
rs.audit()
location_path = urlsplit(r.headers["Location"]).path
tx = HTTPTransaction(graph_name, location_path.rpartition("/")[-1])
self._transactions.add(tx)
self.release()
rs.audit(tx)
return tx

def commit(self, tx):
Expand All @@ -176,8 +176,8 @@ def commit(self, tx):
r = self._post(tx.commit_uri())
assert r.status == 200 # TODO: other codes
rs = HTTPResponse.from_json(r.data.decode("utf-8"))
rs.audit()
self.release()
rs.audit(tx)
return Bookmark()

def rollback(self, tx):
Expand All @@ -186,16 +186,16 @@ def rollback(self, tx):
r = self._delete(tx.uri())
assert r.status == 200 # TODO: other codes
rs = HTTPResponse.from_json(r.data.decode("utf-8"))
rs.audit()
self.release()
rs.audit(tx)
return Bookmark()

def run_in_tx(self, tx, cypher, parameters=None):
r = self._post(tx.uri(), cypher, parameters)
assert r.status == 200 # TODO: other codes
rs = HTTPResponse.from_json(r.data.decode("utf-8"))
rs.audit()
self.release()
rs.audit(tx)
return HTTPResult(tx.graph_name, rs.result(), profile=self.profile)

def pull(self, result, n=-1):
Expand Down Expand Up @@ -243,6 +243,15 @@ def supports_multi(self):

class HTTPTransaction(Transaction):

def __init__(self, graph_name, txid=None, readonly=False):
super(HTTPTransaction, self).__init__(graph_name, txid, readonly)
self.failure = None

def __bool__(self):
return self.failure is None

__nonzero__ = __bool__

@classmethod
def autocommit_uri(cls, graph_name):
if graph_name:
Expand Down Expand Up @@ -346,8 +355,10 @@ def stats(self):
def errors(self):
return self._content.get("errors", [])

def audit(self):
def audit(self, tx=None):
if self.errors():
from py2neo.database.work import GraphError
failure = GraphError.hydrate(self.errors().pop(0))
if tx is not None:
tx.failure = failure
raise failure
1 change: 1 addition & 0 deletions test/integration/test_errors.py
Expand Up @@ -26,6 +26,7 @@ def test_can_generate_transaction_error(graph):
with raises(ClientError) as e:
tx.run("X")
assert e.value.code == "Neo.ClientError.Statement.SyntaxError"
assert tx.finished()
with raises(TransactionFinished):
tx.commit()

Expand Down

0 comments on commit 65c44d9

Please sign in to comment.