gremlingraph-databasesamazon-neptunetinkerpop3gremlinpython

How to avoid StopIteration in gremlin python?


When the expected vertex or edge is not present in the database, gremlin python raises the exception StopIteration. How to resolve/prevent the exception. Query to return none or empty instead of an error.

Eg:

g.V().hasLabel('employee').has('name', 'Thirumal').elementMap().next()

output when the vertex is not available

    def __next__(self):
    if self.traversers is None:
        self.traversal_strategies.apply_strategies(self)
    if self.last_traverser is None:
        self.last_traverser = next(self.traversers)
        StopIteration

  /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gremlin_python/process/traversal.py:50: StopIteration

Solution

  • Instead of using next, use toList instead. Then, if there is no data, you will get back an empty list.