Exceptions in Python driver do not extend RuntimeError

My understanding of Python is that user-defined exceptions are supposed to extend the built-in RuntimeError class.

Here's some code that I expect to work:

def isAvailableDatabase_(self, aDatabaseName):
  cypherQuery = f"""SHOW DATABASE `{aDatabaseName}` YIELD currentStatus"""
  with self.neo4JAdapterPreset() as adapter:
    with adapter.sessionWithParameters_(database='system') as session:
      try:
        result = session.run(cypherQuery)
        record= result.single()
        isAvailable = (record is not None) and ('online' == record['currentStatus'])
      except RuntimeError as runtimeError:
        exceptionStr = str(runtimeError)
        complaint = f"""Raised {exceptionStr}"""
        print(complaint)
  return False

I want this method to simply return False when the database isn't available, such as while Neo4J is not running. The stuff inside the exception handler is just so that I can see what's happening.

I want truly awful and unexpected exceptions to NOT be caught. I don't want to "leak" the Neo4J-specific exceptions from the official Python driver into this code.

Is there a straightforward way to catch an ancestor of the several exceptions defined in the official Python driver for Neo4J?

@tms I believe both the server-side Neo4jError and client side DriverErrors extend the base Exception class and not RuntimeError. If you catch for either of those or for a general exception you should catch errors like:

Couldn't connect to localhost:7687 (resolved to ())

Link to Driver Error docs for reference.

1 Like