It would be nice if there was a way to have a Gremlin query terminate with some kind of error message if, as part of the query, it is determined that there is no point proceeding further. One not very nice way of doing this is to trigger an exception. For example something like this:
gremlin> g.inject(1).math('_/0')
Division by zero!
Is there a better way to trigger a more meaningful exception when a query determines that there is no need continuing?
Starting with the Apache TinkerPop 3.6.0 release (April 2022) a new
failstep is now part of the Gremlin language. So for the simple case in the question, you can do something like this:which when run yields a much nicer exception that an application can catch. When run using the Gremlin console, here is the output from the
failstep.Building on this, a slightly more real world example might be where we expect a node to exist and if we discover it does not, we want to fail immediately.