Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

adding custom error message logic #9

Merged
merged 1 commit into from
Sep 28, 2023
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
3 changes: 2 additions & 1 deletion rdflib_neo4j/Neo4jStore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rdflib_neo4j.config.const import NEO4J_DRIVER_USER_AGENT_NAME
from rdflib_neo4j.query_composers.NodeQueryComposer import NodeQueryComposer
from rdflib_neo4j.query_composers.RelationshipQueryComposer import RelationshipQueryComposer
from rdflib_neo4j.utils import handle_neo4j_driver_exception


class Neo4jStore(Store):
Expand Down Expand Up @@ -309,6 +310,6 @@ def __query_database(self, query, params):
try:
self.session.run(query, params=params)
except Exception as e:
print(e)
e = handle_neo4j_driver_exception(e)
logging.error(e)
raise e
12 changes: 11 additions & 1 deletion rdflib_neo4j/config/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
NEO4J_AUTH_REQUIRED_FIELDS = ["uri", "database", "user", "pwd"]
NEO4J_DRIVER_USER_AGENT_NAME = "neo4j_labs_n10s_client_lib"


class PrefixNotFoundException(Exception):

# Constructor or Initializer
Expand Down Expand Up @@ -51,6 +50,17 @@ def __str__(self):
return f"""Missing {self.param_name} key inside the authentication definition. Remember that it should contain the following keys:
: [uri, database, user, pwd]"""

class CypherMultipleTypesMultiValueException(Exception):

# Constructor or Initializer
def __init__(self):
super().__init__()

def __str__(self):
return f"""Values of a multivalued property must have the same datatype."""

NEO4J_DRIVER_MULTIPLE_TYPE_ERROR_MESSAGE = """{code: Neo.ClientError.Statement.TypeError} {message: Neo4j only supports a subset of Cypher types for storage as singleton or array properties. Please refer to section cypher/syntax/values of the manual for more details.}"""
NEO4J_DRIVER_DICT_MESSAGE = {NEO4J_DRIVER_MULTIPLE_TYPE_ERROR_MESSAGE: CypherMultipleTypesMultiValueException}

class HANDLE_VOCAB_URI_STRATEGY(Enum):
"""
Expand Down
16 changes: 15 additions & 1 deletion rdflib_neo4j/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from time import time
from typing import Dict
from rdflib import URIRef
from rdflib_neo4j.config.const import ShortenStrictException, HANDLE_VOCAB_URI_STRATEGY
from rdflib_neo4j.config.const import ShortenStrictException, HANDLE_VOCAB_URI_STRATEGY, NEO4J_DRIVER_DICT_MESSAGE


def timing(f):
Expand Down Expand Up @@ -163,3 +163,17 @@ def handle_vocab_uri(mappings: Dict[str, str],
elif strategy == HANDLE_VOCAB_URI_STRATEGY.IGNORE:
return handle_vocab_uri_ignore(predicate)
raise Exception(f"Strategy {strategy} not defined.")


def handle_neo4j_driver_exception(ex: Exception):
"""
Handle exceptions raised by the Neo4j driver by providing custom error messages.

Args:
ex (Exception): The exception raised by the Neo4j driver.

Returns:
Exception: A custom exception or the original exception.
"""
res = NEO4J_DRIVER_DICT_MESSAGE.get(str(ex))
return ex if not res else res()