diff --git a/src/glue/SessionHL.cpp b/src/glue/SessionHL.cpp index cc7910d1c..bff12d188 100644 --- a/src/glue/SessionHL.cpp +++ b/src/glue/SessionHL.cpp @@ -234,11 +234,55 @@ std::pair, std::optional> SessionHL::Interpret( throw memgraph::communication::bolt::ClientError(e.what()); } } -void SessionHL::RollbackTransaction() { interpreter_.RollbackTransaction(); } -void SessionHL::CommitTransaction() { interpreter_.CommitTransaction(); } -void SessionHL::BeginTransaction(const std::map &extra) { - interpreter_.BeginTransaction(ToQueryExtras(extra)); + +void SessionHL::RollbackTransaction() { + try { + interpreter_.RollbackTransaction(); + } catch (const memgraph::query::QueryException &e) { + // Count the number of specific exceptions thrown + metrics::IncrementCounter(GetExceptionName(e)); + // Wrap QueryException into ClientError, because we want to allow the + // client to fix their query. + throw memgraph::communication::bolt::ClientError(e.what()); + } catch (const memgraph::query::ReplicationException &e) { + // Count the number of specific exceptions thrown + metrics::IncrementCounter(GetExceptionName(e)); + throw memgraph::communication::bolt::ClientError(e.what()); + } } + +void SessionHL::CommitTransaction() { + try { + interpreter_.CommitTransaction(); + } catch (const memgraph::query::QueryException &e) { + // Count the number of specific exceptions thrown + metrics::IncrementCounter(GetExceptionName(e)); + // Wrap QueryException into ClientError, because we want to allow the + // client to fix their query. + throw memgraph::communication::bolt::ClientError(e.what()); + } catch (const memgraph::query::ReplicationException &e) { + // Count the number of specific exceptions thrown + metrics::IncrementCounter(GetExceptionName(e)); + throw memgraph::communication::bolt::ClientError(e.what()); + } +} + +void SessionHL::BeginTransaction(const std::map &extra) { + try { + interpreter_.BeginTransaction(ToQueryExtras(extra)); + } catch (const memgraph::query::QueryException &e) { + // Count the number of specific exceptions thrown + metrics::IncrementCounter(GetExceptionName(e)); + // Wrap QueryException into ClientError, because we want to allow the + // client to fix their query. + throw memgraph::communication::bolt::ClientError(e.what()); + } catch (const memgraph::query::ReplicationException &e) { + // Count the number of specific exceptions thrown + metrics::IncrementCounter(GetExceptionName(e)); + throw memgraph::communication::bolt::ClientError(e.what()); + } +} + void SessionHL::Configure(const std::map &run_time_info) { #ifdef MG_ENTERPRISE std::string db; diff --git a/tests/drivers/python/v5_8/transactions.py b/tests/drivers/python/v5_8/transactions.py index 9c8dd2f5f..d26eef44c 100644 --- a/tests/drivers/python/v5_8/transactions.py +++ b/tests/drivers/python/v5_8/transactions.py @@ -96,12 +96,12 @@ def clear_db(session): with GraphDatabase.driver("bolt://localhost:7687", auth=None, encrypted=False) as driver: with driver.session() as session: # Clear the DB - session.run("MATCH (n) DETACH DELETE n;") + session.run("MATCH (n) DETACH DELETE n;").consume() # Add constraints - session.run("CREATE CONSTRAINT ON (n:Person) ASSERT n.id IS UNIQUE;") - session.run("CREATE CONSTRAINT ON (n:Employee) ASSERT n.id IS UNIQUE;") - session.run("CREATE CONSTRAINT ON (n:Employee) ASSERT EXISTS (n.id);") + session.run("CREATE CONSTRAINT ON (n:Person) ASSERT n.id IS UNIQUE;").consume() + session.run("CREATE CONSTRAINT ON (n:Employee) ASSERT n.id IS UNIQUE;").consume() + session.run("CREATE CONSTRAINT ON (n:Employee) ASSERT EXISTS (n.id);").consume() # Set the initial graph state session.execute_write(lambda tx: tx.run("CREATE (n:Employee:Person {id: '123', alt_id: '100'}) RETURN n;")) @@ -109,18 +109,18 @@ with GraphDatabase.driver("bolt://localhost:7687", auth=None, encrypted=False) a # Run a transaction that violates a constraint try: session.execute_write(violate_constraint) - except TransientError: + except ClientError: pass else: clear_db(session) - raise Exception("neo4j.exceptions.TransientError should have been thrown!") + raise Exception("neo4j.exceptions.ClientError should have been thrown!") # Run a transaction that violates no constraints even though an intermediate result does try: session.execute_write(violate_constraint_on_intermediate_result) - except TransientError: + except ClientError: clear_db(session) - raise Exception("neo4j.exceptions.TransientError should not have been thrown!") + raise Exception("neo4j.exceptions.ClientError should not have been thrown!") clear_db(session)