server/search: improve error messages

This commit is contained in:
rr- 2016-06-02 21:15:09 +02:00
parent a72f6aa585
commit 7c7adafd82

View file

@ -4,6 +4,13 @@ from szurubooru import db, errors
from szurubooru.func import util from szurubooru.func import util
from szurubooru.search.base_search_config import BaseSearchConfig 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): def _type_transformer(value):
available_values = { available_values = {
'image': db.Post.TYPE_IMAGE, 'image': db.Post.TYPE_IMAGE,
@ -16,11 +23,7 @@ def _type_transformer(value):
'flash': db.Post.TYPE_FLASH, 'flash': db.Post.TYPE_FLASH,
'swf': db.Post.TYPE_FLASH, 'swf': db.Post.TYPE_FLASH,
} }
try: return _enum_transformer(available_values, value)
return available_values[value.lower()]
except KeyError:
raise errors.SearchError('Invalid value: %r. Available values: %r.' % (
value, available_values))
def _safety_transformer(value): def _safety_transformer(value):
available_values = { available_values = {
@ -29,11 +32,7 @@ def _safety_transformer(value):
'questionable': db.Post.SAFETY_SKETCHY, 'questionable': db.Post.SAFETY_SKETCHY,
'unsafe': db.Post.SAFETY_UNSAFE, 'unsafe': db.Post.SAFETY_UNSAFE,
} }
try: return _enum_transformer(available_values, value)
return available_values[value.lower()]
except KeyError:
raise errors.SearchError('Invalid value: %r. Available values: %r.' % (
value, available_values))
class PostSearchConfig(BaseSearchConfig): class PostSearchConfig(BaseSearchConfig):
def create_filter_query(self): def create_filter_query(self):