Move many errors + warnings to properties of Exception classes
This commit is contained in:
		
							parent
							
								
									5d1c2dfc40
								
							
						
					
					
						commit
						fcc671a617
					
				
					 5 changed files with 82 additions and 78 deletions
				
			
		|  | @ -101,17 +101,7 @@ SQL_USER = _sql_dictify(SQL_USER_COLUMNS) | |||
| 
 | ||||
| # Errors and warnings | ||||
| ERROR_DATABASE_OUTOFDATE = 'Database is out-of-date. {current} should be {new}. Please use utilities\\etiquette_upgrader.py' | ||||
| ERROR_INVALID_ACTION = 'Invalid action' | ||||
| ERROR_NO_SUCH_TAG = 'Tag "{tag}" does not exist' | ||||
| ERROR_NO_TAG_GIVEN = 'No tag name supplied' | ||||
| ERROR_SYNONYM_ITSELF = 'Cant apply synonym to itself' | ||||
| ERROR_TAG_TOO_LONG = '"{tag}" is too long' | ||||
| ERROR_TAG_TOO_SHORT = '"{tag}" has too few valid chars' | ||||
| ERROR_RECURSIVE_GROUPING = 'Recursive grouping' | ||||
| WARNING_MINMAX_INVALID = 'Field "{field}": "{value}" is not a valid request. Ignored.' | ||||
| WARNING_MINMAX_OOO = 'Field "{field}": minimum "{min}" maximum "{max}" are out of order. Ignored.' | ||||
| WARNING_NO_SUCH_TAG = 'Tag "{tag}" does not exist. Ignored.' | ||||
| WARNING_NO_SUCH_USER = 'User "{username}" does not exist. 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.' | ||||
|  |  | |||
|  | @ -1,80 +1,117 @@ | |||
| class EtiquetteException(Exception): | ||||
|     pass | ||||
| 
 | ||||
| # NO SUCH | ||||
| class NoSuchAlbum(Exception): | ||||
| class NoSuchAlbum(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_ALBUM' | ||||
|     error_message = 'Album "{album}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| class NoSuchBookmark(Exception): | ||||
| class NoSuchBookmark(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_BOOKMARK' | ||||
|     error_message = 'Bookmark "{bookmark}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| class NoSuchGroup(Exception): | ||||
| class NoSuchGroup(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_GROUP' | ||||
|     error_message = 'Group "{group}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| class NoSuchPhoto(Exception): | ||||
| class NoSuchPhoto(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_PHOTO' | ||||
|     error_message = 'Photo "{photo}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| class NoSuchSynonym(Exception): | ||||
| class NoSuchSynonym(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_SYNONYM' | ||||
|     error_message = 'Synonym "{synonym}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| class NoSuchTag(Exception): | ||||
| class NoSuchTag(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_TAG' | ||||
|     error_message = 'Tag "{tag}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| class NoSuchUser(Exception): | ||||
| class NoSuchUser(EtiquetteException): | ||||
|     error_type = 'NO_SUCH_User' | ||||
|     error_message = 'User "{user}" does not exist.' | ||||
|     pass | ||||
| 
 | ||||
| 
 | ||||
| # EXISTS | ||||
| class GroupExists(Exception): | ||||
| class GroupExists(EtiquetteException): | ||||
|     pass | ||||
| 
 | ||||
| class PhotoExists(Exception): | ||||
| class PhotoExists(EtiquetteException): | ||||
|     pass | ||||
| 
 | ||||
| class TagExists(Exception): | ||||
| class TagExists(EtiquetteException): | ||||
|     pass | ||||
| 
 | ||||
| class UserExists(Exception): | ||||
| class UserExists(EtiquetteException): | ||||
|     error_type = 'USER_EXISTS' | ||||
|     error_message = 'Username "{username}" already exists.' | ||||
|     pass | ||||
| 
 | ||||
| 
 | ||||
| # TAG ERRORS | ||||
| class CantSynonymSelf(Exception): | ||||
| class CantSynonymSelf(EtiquetteException): | ||||
|     error_type = 'TAG_SYNONYM_ITSELF' | ||||
|     error_message = 'Cannot apply synonym to self.' | ||||
|     pass | ||||
| 
 | ||||
| class RecursiveGrouping(Exception): | ||||
| class RecursiveGrouping(EtiquetteException): | ||||
|     error_type = 'RECURSIVE_GROUPING' | ||||
|     error_message = 'Cannot create a group within itself.' | ||||
|     pass | ||||
| 
 | ||||
| class TagTooLong(Exception): | ||||
| class TagTooLong(EtiquetteException): | ||||
|     error_type = 'TAG_TOO_LONG' | ||||
|     error_message = 'Tag "{tag}" is too long.' | ||||
|     pass | ||||
| 
 | ||||
| class TagTooShort(Exception): | ||||
| class TagTooShort(EtiquetteException): | ||||
|     error_type = 'TAG_TOO_SHORT' | ||||
|     error_message = 'Tag "{tag}" has too few valid characters.' | ||||
|     pass | ||||
| 
 | ||||
| 
 | ||||
| # USER ERRORS | ||||
| class InvalidUsernameChars(Exception): | ||||
| class InvalidUsernameChars(EtiquetteException): | ||||
|     error_type = 'INVALID_USERNAME_CHARACTERS' | ||||
|     error_message = 'Username "{username}" contains invalid characters: {badchars}' | ||||
|     pass | ||||
| 
 | ||||
| class PasswordTooShort(Exception): | ||||
| class PasswordTooShort(EtiquetteException): | ||||
|     error_type = 'PASSWORD_TOO_SHORT' | ||||
|     error_message = 'Password is shorter than the minimum of {min_length}' | ||||
|     pass | ||||
| 
 | ||||
| class UsernameTooLong(Exception): | ||||
| class UsernameTooLong(EtiquetteException): | ||||
|     error_type = 'USERNAME_TOO_LONG' | ||||
|     error_message = 'Username "{username}" is longer than maximum of {max_length}' | ||||
|     pass | ||||
| 
 | ||||
| class UsernameTooShort(Exception): | ||||
| class UsernameTooShort(EtiquetteException): | ||||
|     error_type = 'USERNAME_TOO_SHORT' | ||||
|     error_message = 'Username "{username}" is shorter than minimum of {min_length}' | ||||
|     pass | ||||
| 
 | ||||
| class WrongLogin(Exception): | ||||
| class WrongLogin(EtiquetteException): | ||||
|     pass | ||||
| 
 | ||||
| 
 | ||||
| # GENERAL ERRORS | ||||
| class NotExclusive(Exception): | ||||
| class NotExclusive(EtiquetteException): | ||||
|     ''' | ||||
|     For when two or more mutually exclusive actions have been requested. | ||||
|     ''' | ||||
|     pass | ||||
| 
 | ||||
| class OutOfOrder(Exception): | ||||
| class OutOfOrder(EtiquetteException): | ||||
|     ''' | ||||
|     For when a requested range (a, b) has b > a | ||||
|     For when a requested minmax range (a, b) has b > a | ||||
|     ''' | ||||
|     error_type = 'OUT_OF_ORDER' | ||||
|     error_message = 'Field "{field}": minimum "{min}" and maximum "{max}" are out of order.' | ||||
|     pass | ||||
|  |  | |||
|  | @ -723,7 +723,7 @@ class PDBPhotoMixin: | |||
|                 if node.token in frozen_children: | ||||
|                     continue | ||||
|                 if warning_bag is not None: | ||||
|                     warning_bag.add(constants.WARNING_NO_SUCH_TAG.format(tag=node.token)) | ||||
|                     warning_bag.add(exceptions.NoSuchTag.error_message.format(tag=node.token)) | ||||
|                     node.token = None | ||||
|                 else: | ||||
|                     raise_no_such_thing(exceptions.NoSuchTag, thing_name=node.token) | ||||
|  | @ -924,13 +924,15 @@ class PDBTagMixin: | |||
| 
 | ||||
|         if len(tagname) < self.config['min_tag_name_length']: | ||||
|             if warning_bag is not None: | ||||
|                 warning_bag.add(constants.WARNING_TAG_TOO_SHORT.format(tag=tagname)) | ||||
|                 warning_bag.add(exceptions.TagTooShort.error_message.format(tag=tagname)) | ||||
|                 return None | ||||
|             else: | ||||
|                 raise exceptions.TagTooShort(tagname) | ||||
| 
 | ||||
|         elif len(tagname) > self.config['max_tag_name_length']: | ||||
|             if warning_bag is not None: | ||||
|                 warning_bag.add(constants.WARNING_TAG_TOO_LONG.format(tag=tagname)) | ||||
|                 warning_bag.add(exceptions.TagTooLong.format(tag=tagname)) | ||||
|                 return None | ||||
|             else: | ||||
|                 raise exceptions.TagTooLong(tagname) | ||||
| 
 | ||||
|  |  | |||
|  | @ -73,14 +73,14 @@ def minmax(key, value, minimums, maximums, warning_bag=None): | |||
|             warning_bag.add(constants.WARNING_MINMAX_INVALID.format(field=key, value=value)) | ||||
|             return | ||||
|         else: | ||||
|             raise e | ||||
|             raise | ||||
| 
 | ||||
|     except exceptions.OutOfOrder as e: | ||||
|         if warning_bag: | ||||
|             warning_bag.add(constants.WARNING_MINMAX_OOO.format(field=key, min=e.args[1], max=e.args[2])) | ||||
|             warning_bag.add(e.error_message.format(field=key, min=e.args[1], max=e.args[2])) | ||||
|             return | ||||
|         else: | ||||
|             raise e | ||||
|             raise | ||||
| 
 | ||||
|     if low is not None: | ||||
|         minimums[key] = low | ||||
|  | @ -115,7 +115,7 @@ def normalize_authors(authors, photodb, warning_bag=None): | |||
|             user = get_user(photodb, requested_author) | ||||
|         except exceptions.NoSuchUser: | ||||
|             if warning_bag: | ||||
|                 warning_bag.add(constants.WARNING_NO_SUCH_USER.format(username=requested_author)) | ||||
|                 warning_bag.add(exceptions.NoSuchUser.format(username=requested_author)) | ||||
|             else: | ||||
|                 raise | ||||
|         else: | ||||
|  | @ -307,7 +307,7 @@ def normalize_tag_mmf(tags, photodb, warning_bag=None): | |||
|             tag = photodb.get_tag(name=tag) | ||||
|         except exceptions.NoSuchTag: | ||||
|             if warning_bag: | ||||
|                 warning_bag.add(constants.WARNING_NO_SUCH_TAG.format(tag=tag)) | ||||
|                 warning_bag.add(exceptions.NoSuchTag.format(tag=tag)) | ||||
|                 continue | ||||
|             else: | ||||
|                 raise | ||||
|  |  | |||
|  | @ -239,21 +239,12 @@ def post_register(): | |||
| 
 | ||||
|     try: | ||||
|         user = P.register_user(username, password_1) | ||||
|     except exceptions.UsernameTooShort as e: | ||||
|         error = 'Username shorter than minimum of %d' % P.config['min_username_length'] | ||||
|     except exceptions.UsernameTooLong as e: | ||||
|         error = 'Username longer than maximum of %d' % P.config['max_username_length'] | ||||
|     except exceptions.InvalidUsernameChars as e: | ||||
|         error = 'Username contains invalid characters %s' % e.args[0] | ||||
|     except exceptions.PasswordTooShort as e: | ||||
|         error = 'Password is shorter than minimum of %d' % P.config['min_password_length'] | ||||
|     except exceptions.UserExists as e: | ||||
|         error = 'User %s already exists' % e.args[0] | ||||
|     else: | ||||
|         error = None | ||||
| 
 | ||||
|     if error is not None: | ||||
|         return jsonify.make_json_response({'error': error}, status=422) | ||||
|     except EtiquetteException as e: | ||||
|         response = { | ||||
|             'error_type': e.error_type, | ||||
|             'error_message': e.error_message, | ||||
|         } | ||||
|         return jsonify.make_json_response(response, status=400) | ||||
| 
 | ||||
|     session = sessions.Session(request, user) | ||||
|     session_manager.add(session) | ||||
|  | @ -684,29 +675,13 @@ def post_photo_remove_tag(photoid): | |||
| def post_tag_create_delete_core(tagname, function): | ||||
|     try: | ||||
|         response = function(tagname) | ||||
|     except exceptions.TagTooLong: | ||||
|         error_type = 'TAG_TOO_LONG' | ||||
|         error_message = constants.ERROR_TAG_TOO_LONG.format(tag=tagname) | ||||
|     except exceptions.TagTooShort: | ||||
|         error_type = 'TAG_TOO_SHORT' | ||||
|         error_message = constants.ERROR_TAG_TOO_SHORT.format(tag=tagname) | ||||
|     except exceptions.CantSynonymSelf: | ||||
|         error_type = 'TAG_SYNONYM_ITSELF' | ||||
|         error_message = constants.ERROR_SYNONYM_ITSELF | ||||
|     except exceptions.RecursiveGrouping as e: | ||||
|         error_type = 'RECURSIVE_GROUPING' | ||||
|         error_message = constants.ERROR_RECURSIVE_GROUPING | ||||
|     except exceptions.NoSuchTag as e: | ||||
|         error_type = 'NO_SUCH_TAG' | ||||
|         error_message = constants.ERROR_NO_SUCH_TAG.format(tag=tagname) | ||||
|     else: | ||||
|         error_type = None | ||||
|      | ||||
|     if error_type is not None: | ||||
|         status = 400 | ||||
|         response = {'error_type': error_type, 'error_message': error_message} | ||||
|     else: | ||||
|         status = 200 | ||||
|     except exceptions.EtiquetteException as e: | ||||
|         response = { | ||||
|             'error_type': e.error_type, | ||||
|             'error_message': e.error_message, | ||||
|         } | ||||
|         status = 400 | ||||
| 
 | ||||
|     return jsonify.make_json_response(response, status=status) | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue