diff --git a/etiquette/exceptions.py b/etiquette/exceptions.py index e942f3e..97a2ba5 100644 --- a/etiquette/exceptions.py +++ b/etiquette/exceptions.py @@ -187,6 +187,9 @@ class FeatureDisabled(EtiquetteException): ''' error_message = 'This feature has been disabled. Requires {requires}.' +class GenerateIDFailed(EtiquetteException): + error_message = 'Could not generate unique ID on {table}. You should increase ID bits.' + class MinMaxInvalid(EtiquetteException): ''' For when the user searches for e.g. width=a-b but the a-b can't be parsed. diff --git a/etiquette/photodb.py b/etiquette/photodb.py index cdb8efa..9d9d00f 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1236,10 +1236,11 @@ class PhotoDB( table = thing_class.table length = self.config['id_bits'] - while True: + for retry in range(10): id = RNG.getrandbits(length) if not self.exists(f'SELECT 1 FROM {table} WHERE id == ?', [id]): return id + raise exceptions.GenerateIDFailed(table=table) def load_config(self) -> None: log.debug('Loading config file.')