Not able to catch CollectionInvalid exception

database.validate_collection

The functionvalidate_collection is supposed to raise CollectionInvalid when collection name is invalid as per documentation. However,I when try to catch exception, it does not reach that exception block. Instead I need to trap OperationFailure error object and use code=26 determine invalid collection name.

        try:
            client = pymongo.MongoClient(...)
            db = client.get_default_database()
            db.validate_collection(wrong_name)
        except OperationFailure as e:
            print(e.code) # <-- reaches
        except CollectionInvalid as e:
            print("Invalid") # <-- does not reach

Is this expected behavior?

PyMongo : v3.11.3
Pytthon : v3.8.0

If the collection does not exist it cannot be validated, hence the OperationFailure.

Collection validation is NOT whether or not the collection name is correct.

1 Like