Add exceptions to warning_bag instead of strings for some.

I was adding messages as strings because that's how they get shown on
the web interface. But it's better to return the real exception objects
and have the interface deal with it.
This commit is contained in:
voussoir 2021-01-01 12:17:15 -08:00
parent 94130e4803
commit d3f6d6b26a
2 changed files with 19 additions and 13 deletions

View file

@ -107,7 +107,7 @@ def minmax(key, value, minimums, maximums, warning_bag=None):
except exceptions.OutOfOrder as exc: except exceptions.OutOfOrder as exc:
if warning_bag: if warning_bag:
warning_bag.add(exc.error_message) warning_bag.add(exc)
return return
else: else:
raise raise
@ -146,7 +146,7 @@ def normalize_author(authors, photodb, warning_bag=None):
user = photodb.get_user(username=requested_author) user = photodb.get_user(username=requested_author)
except exceptions.NoSuchUser as exc: except exceptions.NoSuchUser as exc:
if warning_bag: if warning_bag:
warning_bag.add(exc.error_message) warning_bag.add(exc)
else: else:
raise raise
else: else:
@ -255,7 +255,7 @@ def normalize_mmf_vs_expression_conflict(
if (tag_musts or tag_mays or tag_forbids) and tag_expression: if (tag_musts or tag_mays or tag_forbids) and tag_expression:
exc = exceptions.NotExclusive(['tag_musts+mays+forbids', 'tag_expression']) exc = exceptions.NotExclusive(['tag_musts+mays+forbids', 'tag_expression'])
if warning_bag: if warning_bag:
warning_bag.add(exc.error_message) warning_bag.add(exc)
else: else:
raise exc raise exc
conflict = True conflict = True
@ -317,19 +317,19 @@ def normalize_orderby(orderby, warning_bag=None):
direction = 'desc' direction = 'desc'
else: else:
message = constants.WARNING_ORDERBY_INVALID.format(request=requested_order) exc = ValueError(constants.WARNING_ORDERBY_INVALID.format(request=requested_order))
if warning_bag: if warning_bag:
warning_bag.add(message) warning_bag.add(exc)
else: else:
raise ValueError(message) raise exc
continue continue
if column not in constants.ALLOWED_ORDERBY_COLUMNS: if column not in constants.ALLOWED_ORDERBY_COLUMNS:
message = constants.WARNING_ORDERBY_BADCOL.format(column=column) exc = ValueError(constants.WARNING_ORDERBY_BADCOL.format(column=column))
if warning_bag: if warning_bag:
warning_bag.add(message) warning_bag.add(exc)
else: else:
raise ValueError(message) raise exc
continue continue
if column == 'random': if column == 'random':
@ -352,10 +352,11 @@ def normalize_orderby(orderby, warning_bag=None):
column=column, column=column,
direction=direction, direction=direction,
) )
exc = ValueError(message)
if warning_bag: if warning_bag:
warning_bag.add(message) warning_bag.add(exc)
else: else:
raise ValueError(message) raise exc
direction = 'desc' direction = 'desc'
requested_order = (column, direction) requested_order = (column, direction)
@ -492,7 +493,7 @@ def normalize_tagset(photodb, tags, warning_bag=None):
tag = photodb.get_tag(name=tag) tag = photodb.get_tag(name=tag)
except exceptions.NoSuchTag as exc: except exceptions.NoSuchTag as exc:
if warning_bag: if warning_bag:
warning_bag.add(exc.error_message) warning_bag.add(exc)
continue continue
else: else:
raise exc raise exc
@ -519,7 +520,7 @@ def tag_expression_tree_builder(
node.token = photodb.get_tag(name=node.token).name node.token = photodb.get_tag(name=node.token).name
except (exceptions.NoSuchTag) as exc: except (exceptions.NoSuchTag) as exc:
if warning_bag: if warning_bag:
warning_bag.add(exc.error_message) warning_bag.add(exc)
node.token = None node.token = None
else: else:
raise raise

View file

@ -403,6 +403,11 @@ def get_search_core():
continue continue
search_results.append(item) search_results.append(item)
warnings = [
w.error_message if hasattr(w, 'error_message') else str(w)
for w in warnings
]
# TAGS ON THIS PAGE # TAGS ON THIS PAGE
total_tags = set() total_tags = set()
for result in search_results: for result in search_results: