Skip to content

Commit 7e1a748

Browse files
committed
Improve Noe4jError representation
When the error is not received from the DBMS, but instead originates from somewhere in the driver, it might not have a code and a message. In that case, we fall back to the default Exception representation. Related: * #796
1 parent 8971b29 commit 7e1a748

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

neo4j/exceptions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,17 @@ def is_fatal_during_discovery(self) -> bool:
247247
return False
248248

249249
def __str__(self):
250-
return "{{code: {code}}} {{message: {message}}}".format(code=self.code, message=self.message)
250+
if self.code and self.message:
251+
return "{{code: {code}}} {{message: {message}}}".format(
252+
code=self.code, message=self.message
253+
)
254+
return super().__str__()
251255

252256

253257
# Neo4jError > ClientError
254258
class ClientError(Neo4jError):
255259
""" The Client sent a bad request - changing the request might yield a successful outcome.
256260
"""
257-
def __str__(self) -> str:
258-
return super().__str__()
259261

260262

261263
# Neo4jError > ClientError > CypherSyntaxError

tests/unit/common/test_exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,20 @@ def test_error_rewrite(code, expected_cls, expected_code):
257257
assert error.is_retryable() is expected_retryable
258258
with pytest.warns(DeprecationWarning, match=".*is_retryable.*"):
259259
assert error.is_retriable() is expected_retryable
260+
261+
262+
def test_neo4j_error_from_server_as_str():
263+
error = Neo4jError.hydrate(message="Test error message",
264+
code="Neo.ClientError.General.UnknownError")
265+
266+
assert isinstance(error, ClientError)
267+
assert str(error) == ("{code: Neo.ClientError.General.UnknownError} "
268+
"{message: Test error message}")
269+
270+
271+
@pytest.mark.parametrize("cls", (Neo4jError, ClientError))
272+
def test_neo4j_error_from_code_as_str(cls):
273+
error = cls("Generated somewhere in the driver")
274+
275+
assert isinstance(error, cls)
276+
assert str(error) == "Generated somewhere in the driver"

0 commit comments

Comments
 (0)