Skip to content

Commit 97abbab

Browse files
committed
Make Neo4jError representation more robust
Even though, all errors that come from the server go through `Noej4jError.hydrate` which (currently) makes sure that `code` *and* `message` are set, this might change in the future.
1 parent 7e1a748 commit 97abbab

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

neo4j/exceptions.py

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

249249
def __str__(self):
250-
if self.code and self.message:
250+
if self.code or self.message:
251251
return "{{code: {code}}} {{message: {message}}}".format(
252-
code=self.code, message=self.message
252+
code=self.code, message=super().__str__()
253253
)
254254
return super().__str__()
255255

tests/unit/common/test_exceptions.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,57 @@ def test_error_rewrite(code, expected_cls, expected_code):
259259
assert error.is_retriable() is expected_retryable
260260

261261

262-
def test_neo4j_error_from_server_as_str():
263-
error = Neo4jError.hydrate(message="Test error message",
264-
code="Neo.ClientError.General.UnknownError")
262+
@pytest.mark.parametrize(
263+
("code", "message", "expected_cls", "expected_str"),
264+
(
265+
(
266+
"Neo.ClientError.General.UnknownError",
267+
"Test error message",
268+
ClientError,
269+
"{code: Neo.ClientError.General.UnknownError} "
270+
"{message: Test error message}"
271+
),
272+
(
273+
None,
274+
"Test error message",
275+
DatabaseError,
276+
"{code: Neo.DatabaseError.General.UnknownError} "
277+
"{message: Test error message}"
278+
),
279+
(
280+
"",
281+
"Test error message",
282+
DatabaseError,
283+
"{code: Neo.DatabaseError.General.UnknownError} "
284+
"{message: Test error message}"
285+
),
286+
(
287+
"Neo.ClientError.General.UnknownError",
288+
None,
289+
ClientError,
290+
"{code: Neo.ClientError.General.UnknownError} "
291+
"{message: An unknown error occurred}"
292+
),
293+
(
294+
"Neo.ClientError.General.UnknownError",
295+
"",
296+
ClientError,
297+
"{code: Neo.ClientError.General.UnknownError} "
298+
"{message: An unknown error occurred}"
299+
),
300+
)
301+
)
302+
def test_neo4j_error_from_server_as_str(code, message, expected_cls,
303+
expected_str):
304+
error = Neo4jError.hydrate(code=code, message=message)
265305

266-
assert isinstance(error, ClientError)
267-
assert str(error) == ("{code: Neo.ClientError.General.UnknownError} "
268-
"{message: Test error message}")
306+
assert type(error) == expected_cls
307+
assert str(error) == expected_str
269308

270309

271310
@pytest.mark.parametrize("cls", (Neo4jError, ClientError))
272311
def test_neo4j_error_from_code_as_str(cls):
273312
error = cls("Generated somewhere in the driver")
274313

275-
assert isinstance(error, cls)
314+
assert type(error)== cls
276315
assert str(error) == "Generated somewhere in the driver"

0 commit comments

Comments
 (0)