Replace these warning strings with proper exception classes.

master
voussoir 2021-10-18 14:17:20 -07:00
parent 81640102d5
commit b3ed94c22b
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
4 changed files with 39 additions and 21 deletions

View File

@ -214,15 +214,6 @@ ALLOWED_ORDERBY_COLUMNS = {
'width', 'width',
} }
# Errors and warnings ##############################################################################
WARNING_MINMAX_INVALID = 'Field "{field}": "{value}" is not a valid request. Ignored.'
WARNING_ORDERBY_INVALID = 'Invalid orderby request "{request}". Ignored.'
WARNING_ORDERBY_BADCOL = '"{column}" is not a sorting option. Ignored.'
WARNING_ORDERBY_BADDIRECTION = '''
You can\'t order "{column}" by "{direction}". Defaulting to descending.
'''
# Janitorial stuff ################################################################################# # Janitorial stuff #################################################################################
FILENAME_BADCHARS = '\\/:*?<>|"' FILENAME_BADCHARS = '\\/:*?<>|"'

View File

@ -187,6 +187,19 @@ class FeatureDisabled(EtiquetteException):
''' '''
error_message = 'This feature has been disabled. Requires {requires}.' error_message = 'This feature has been disabled. Requires {requires}.'
class MinMaxInvalid(EtiquetteException):
'''
For when the user searches for e.g. width=a-b but the a-b can't be parsed.
If the values can be parsed but are backward, use MinMaxOutOfOrder.
'''
error_message = 'Field "{field}": "{value}" is not a valid request.'
class MinMaxOutOfOrder(EtiquetteException):
'''
For when a requested minmax range (a, b) has b > a
'''
error_message = 'Range "{range}": minimum "{min}" and maximum "{max}" are out of order.'
class NoClosestPhotoDB(EtiquetteException): class NoClosestPhotoDB(EtiquetteException):
''' '''
For calls to PhotoDB.closest_photodb where none exists between cwd and For calls to PhotoDB.closest_photodb where none exists between cwd and
@ -207,8 +220,25 @@ class NotExclusive(EtiquetteException):
''' '''
error_message = 'One and only one of {} must be passed.' error_message = 'One and only one of {} must be passed.'
class OutOfOrder(EtiquetteException): class OrderByBadColumn(EtiquetteException):
''' '''
For when a requested minmax range (a, b) has b > a For when the user tries to orderby a column that does not exist or is
not allowed.
''' '''
error_message = 'Range "{range}": minimum "{min}" and maximum "{max}" are out of order.' error_message = '"{column}" is not a sortable column.'
class OrderByBadDirection(EtiquetteException):
'''
For when the user tries to orderby a direction that is not asc or desc.
'''
error_message = 'You can\'t order "{column}" by "{direction}". Should be asc or desc.'
class OrderByInvalid(EtiquetteException):
'''
For when the orderby request cannot be parsed into column and direction.
For example, it contains too many hyphens like a-b-c.
If the column and direction can be parsed but are invalid, use
OrderByBadColumn or OrderByBadDirection
'''
error_message = 'Invalid orderby request "{request}".'

View File

@ -294,7 +294,7 @@ def hyphen_range(s) -> tuple:
high = parse_unit_string(high) high = parse_unit_string(high)
if low is not None and high is not None and low > high: if low is not None and high is not None and low > high:
raise exceptions.OutOfOrder(range=s, min=low, max=high) raise exceptions.MinMaxOutOfOrder(range=s, min=low, max=high)
return (low, high) return (low, high)

View File

@ -100,7 +100,8 @@ def minmax(key, value, minimums, maximums, warning_bag=None):
except ValueError as exc: except ValueError as exc:
if warning_bag: if warning_bag:
warning_bag.add(constants.WARNING_MINMAX_INVALID.format(field=key, value=value)) exc = exceptions.MinMaxInvalid(field=key, value=value)
warning_bag.add(exc)
return return
else: else:
raise raise
@ -320,7 +321,7 @@ def normalize_orderby(orderby, warning_bag=None):
direction = 'desc' direction = 'desc'
else: else:
exc = ValueError(constants.WARNING_ORDERBY_INVALID.format(request=requested_order)) exc = exceptions.OrderByInvalid(request=requested_order)
if warning_bag: if warning_bag:
warning_bag.add(exc) warning_bag.add(exc)
continue continue
@ -328,11 +329,7 @@ def normalize_orderby(orderby, warning_bag=None):
raise exc raise exc
if direction not in ('asc', 'desc'): if direction not in ('asc', 'desc'):
message = constants.WARNING_ORDERBY_BADDIRECTION.format( exc = exceptions.OrderByBadDirection(column=column, direction=direction)
column=column,
direction=direction,
)
exc = ValueError(message)
if warning_bag: if warning_bag:
warning_bag.add(exc) warning_bag.add(exc)
direction = 'desc' direction = 'desc'
@ -340,7 +337,7 @@ def normalize_orderby(orderby, warning_bag=None):
raise exc raise exc
if column not in constants.ALLOWED_ORDERBY_COLUMNS: if column not in constants.ALLOWED_ORDERBY_COLUMNS:
exc = ValueError(constants.WARNING_ORDERBY_BADCOL.format(column=column)) exc = exceptions.OrderByBadColumn(column=column)
if warning_bag: if warning_bag:
warning_bag.add(exc) warning_bag.add(exc)
continue continue