Skip to content

Commit 57daeea

Browse files
committed
Provide sensible errors when a closed session is used.
1 parent 5711d2d commit 57daeea

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

neo4j/v1/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ 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
"""

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: 20 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, \
2929
Node, Relationship, Path
3030
from neo4j.v1.types import Record
3131

@@ -673,3 +673,22 @@ def test_should_sync_after_rollback(self):
673673
buffer = result._records
674674
assert len(buffer) == 1
675675
assert buffer[0][0] == 1
676+
677+
678+
class SessionClosedTestCase(ServerTestCase):
679+
680+
def setUp(self):
681+
self.driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN)
682+
self.session = self.driver.session()
683+
self.session.close()
684+
685+
def tearDown(self):
686+
self.driver.close()
687+
688+
def test_errors_on_run(self):
689+
with self.assertRaises(SessionError):
690+
self.session.run("RETURN 1")
691+
692+
def test_errors_on_begin_transaction(self):
693+
with self.assertRaises(SessionError):
694+
self.session.begin_transaction()

0 commit comments

Comments
 (0)