From b99c5949316a1faf384388e70277d38d645b214f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 11 Nov 2022 15:29:22 -0800 Subject: [PATCH] Add exceptions.GenerateIDFailed. --- etiquette/exceptions.py | 3 +++ etiquette/photodb.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) 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.')