Skip to content

Commit 6077528

Browse files
authored
Merge pull request #119 from srbaker/1.1-sensible-errors-on-sessions-and-transactions
sensible errors on sessions and transactions
2 parents 8a3f105 + 396caab commit 6077528

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

neo4j/v1/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,17 @@ def __init__(self, data):
423423
setattr(self, key, value)
424424

425425

426+
class SessionError(Exception):
427+
""" Raised when an error occurs while using a session.
428+
"""
429+
430+
426431
class TransactionError(Exception):
427432
""" Raised when an error occurs while using a transaction.
428433
"""
429434

430435

431-
class SessionExpired(Exception):
436+
class SessionExpired(SessionError):
432437
""" Raised when no a session is no longer able to fulfil
433438
its purpose.
434439
"""

neo4j/v1/bolt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .api import GraphDatabase, Driver, Session, StatementResult, \
2525
READ_ACCESS, WRITE_ACCESS, \
2626
fix_statement, fix_parameters, \
27-
CypherError, SessionExpired
27+
CypherError, SessionExpired, SessionError
2828
from .routing import RoutingConnectionPool
2929
from .security import SecurityPlan
3030
from .summary import ResultSummary
@@ -131,6 +131,9 @@ def run(self, statement, parameters=None, **kwparameters):
131131
:return: Cypher result
132132
:rtype: :class:`.StatementResult`
133133
"""
134+
if not self.connection:
135+
raise SessionError("This session is closed.")
136+
134137
self.last_bookmark = None
135138

136139
statement = fix_statement(statement)

test/test_session.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from neo4j.v1 import \
2727
GraphDatabase, basic_auth, READ_ACCESS, WRITE_ACCESS, \
28-
CypherError, \
28+
CypherError, SessionError, TransactionError, \
2929
Node, Relationship, Path
3030
from neo4j.v1.types import Record
3131

@@ -679,3 +679,39 @@ def test_should_sync_after_rollback(self):
679679
buffer = result._records
680680
assert len(buffer) == 1
681681
assert buffer[0][0] == 1
682+
683+
684+
class SessionClosedTestCase(ServerTestCase):
685+
686+
def setUp(self):
687+
self.driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN)
688+
self.session = self.driver.session()
689+
self.session.close()
690+
691+
def tearDown(self):
692+
self.driver.close()
693+
694+
def test_errors_on_run(self):
695+
with self.assertRaises(SessionError):
696+
self.session.run("RETURN 1")
697+
698+
def test_errors_on_begin_transaction(self):
699+
with self.assertRaises(SessionError):
700+
self.session.begin_transaction()
701+
702+
703+
class TransactionCommittedTestCase(ServerTestCase):
704+
705+
def setUp(self):
706+
self.driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN)
707+
self.session = self.driver.session()
708+
self.transaction = self.session.begin_transaction()
709+
self.transaction.run("RETURN 1")
710+
self.transaction.commit()
711+
712+
def tearDown(self):
713+
self.driver.close()
714+
715+
def test_errors_on_run(self):
716+
with self.assertRaises(TransactionError):
717+
self.transaction.run("RETURN 1")

0 commit comments

Comments
 (0)