Skip to content

[4.4] Improve Neo4jError representation #805

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions neo4j/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ def is_fatal_during_discovery(self):
return False

def __str__(self):
return "{{code: {code}}} {{message: {message}}}".format(code=self.code, message=self.message)
if self.code or self.message:
return "{{code: {code}}} {{message: {message}}}".format(
code=self.code, message=self.message
)
return super().__str__()


class ClientError(Neo4jError):
""" The Client sent a bad request - changing the request might yield a successful outcome.
"""
def __str__(self):
return super(Neo4jError, self).__str__()


class DatabaseError(Neo4jError):
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,59 @@ def test_transient_error_is_retriable_case_3():

assert isinstance(error, TransientError)
assert error.is_retriable() is True


@pytest.mark.parametrize(
("code", "message", "expected_cls", "expected_str"),
(
(
"Neo.ClientError.General.UnknownError",
"Test error message",
ClientError,
"{code: Neo.ClientError.General.UnknownError} "
"{message: Test error message}"
),
(
None,
"Test error message",
DatabaseError,
"{code: Neo.DatabaseError.General.UnknownError} "
"{message: Test error message}"
),
(
"",
"Test error message",
DatabaseError,
"{code: Neo.DatabaseError.General.UnknownError} "
"{message: Test error message}"
),
(
"Neo.ClientError.General.UnknownError",
None,
ClientError,
"{code: Neo.ClientError.General.UnknownError} "
"{message: An unknown error occurred}"
),
(
"Neo.ClientError.General.UnknownError",
"",
ClientError,
"{code: Neo.ClientError.General.UnknownError} "
"{message: An unknown error occurred}"
),
)
)
def test_neo4j_error_from_server_as_str(code, message, expected_cls,
expected_str):
error = Neo4jError.hydrate(code=code, message=message)

assert type(error) == expected_cls
assert str(error) == expected_str


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

assert type(error)== cls
assert str(error) == "Generated somewhere in the driver"