From 7c7adafd82455a8f75080a69264940cce2a2578b Mon Sep 17 00:00:00 2001 From: rr- Date: Thu, 2 Jun 2016 21:15:09 +0200 Subject: [PATCH] server/search: improve error messages --- .../szurubooru/search/post_search_config.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/server/szurubooru/search/post_search_config.py b/server/szurubooru/search/post_search_config.py index 820ae9b5..0797491c 100644 --- a/server/szurubooru/search/post_search_config.py +++ b/server/szurubooru/search/post_search_config.py @@ -4,6 +4,13 @@ from szurubooru import db, errors from szurubooru.func import util from szurubooru.search.base_search_config import BaseSearchConfig +def _enum_transformer(available_values, value): + try: + return available_values[value.lower()] + except KeyError: + raise errors.SearchError('Invalid value: %r. Possible values: %r.' % ( + value, list(sorted(available_values.keys())))) + def _type_transformer(value): available_values = { 'image': db.Post.TYPE_IMAGE, @@ -16,11 +23,7 @@ def _type_transformer(value): 'flash': db.Post.TYPE_FLASH, 'swf': db.Post.TYPE_FLASH, } - try: - return available_values[value.lower()] - except KeyError: - raise errors.SearchError('Invalid value: %r. Available values: %r.' % ( - value, available_values)) + return _enum_transformer(available_values, value) def _safety_transformer(value): available_values = { @@ -29,11 +32,7 @@ def _safety_transformer(value): 'questionable': db.Post.SAFETY_SKETCHY, 'unsafe': db.Post.SAFETY_UNSAFE, } - try: - return available_values[value.lower()] - except KeyError: - raise errors.SearchError('Invalid value: %r. Available values: %r.' % ( - value, available_values)) + return _enum_transformer(available_values, value) class PostSearchConfig(BaseSearchConfig): def create_filter_query(self):