server/errors: add and document error codes
This commit is contained in:
		
							parent
							
								
									8674c8b50e
								
							
						
					
					
						commit
						16d04adde0
					
				
							
								
								
									
										47
									
								
								API.md
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								API.md
									
									
									
									
									
								
							@ -120,11 +120,58 @@ code together with JSON of following structure:
 | 
			
		||||
 | 
			
		||||
```json5
 | 
			
		||||
{
 | 
			
		||||
    "name": "Name of the error, e.g. 'PostNotFoundError'",
 | 
			
		||||
    "title": "Generic title of error message, e.g. 'Not found'",
 | 
			
		||||
    "description": "Detailed description of what went wrong, e.g. 'User `rr-` not found."
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
List of possible error names:
 | 
			
		||||
 | 
			
		||||
- `MissingRequiredFileError`
 | 
			
		||||
- `MissingRequiredParameterError`
 | 
			
		||||
- `InvalidParameterError` (when trying to pass text when integer is expected etc.)
 | 
			
		||||
- `IntegrityError` (race conditions when editing the same resource)
 | 
			
		||||
- `SearchError`
 | 
			
		||||
- `AuthError`
 | 
			
		||||
- `PostNotFoundError`
 | 
			
		||||
- `PostAlreadyFeaturedError`
 | 
			
		||||
- `PostAlreadyUploadedError`
 | 
			
		||||
- `InvalidPostIdError`
 | 
			
		||||
- `InvalidPostSafetyError`
 | 
			
		||||
- `InvalidPostSourceError`
 | 
			
		||||
- `InvalidPostContentError`
 | 
			
		||||
- `InvalidPostRelationError`
 | 
			
		||||
- `InvalidPostNoteError`
 | 
			
		||||
- `InvalidPostFlagError`
 | 
			
		||||
- `InvalidFavoriteTargetError`
 | 
			
		||||
- `InvalidCommentIdError`
 | 
			
		||||
- `CommentNotFoundError`
 | 
			
		||||
- `EmptyCommentTextError`
 | 
			
		||||
- `InvalidScoreTargetError`
 | 
			
		||||
- `InvalidScoreValueError`
 | 
			
		||||
- `TagCategoryNotFoundError`
 | 
			
		||||
- `TagCategoryAlreadyExistsError`
 | 
			
		||||
- `TagCategoryIsInUseError`
 | 
			
		||||
- `InvalidTagCategoryNameError`
 | 
			
		||||
- `InvalidTagCategoryColorError`
 | 
			
		||||
- `TagNotFoundError`
 | 
			
		||||
- `TagAlreadyExistsError`
 | 
			
		||||
- `TagIsInUseError`
 | 
			
		||||
- `InvalidTagNameError`
 | 
			
		||||
- `InvalidTagRelationError`
 | 
			
		||||
- `InvalidTagCategoryError`
 | 
			
		||||
- `InvalidTagDescriptionError`
 | 
			
		||||
- `UserNotFoundError`
 | 
			
		||||
- `UserAlreadyExistsError`
 | 
			
		||||
- `InvalidUserNameError`
 | 
			
		||||
- `InvalidEmailError`
 | 
			
		||||
- `InvalidPasswordError`
 | 
			
		||||
- `InvalidRankError`
 | 
			
		||||
- `InvalidAvatarError`
 | 
			
		||||
- `ValidationError` (catch all for odd validation errors)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Field selecting
 | 
			
		||||
 | 
			
		||||
For performance considerations, sometimes the client might want to choose the
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,7 @@ from szurubooru import api, middleware
 | 
			
		||||
 | 
			
		||||
def _map_error(ex, target_class, title):
 | 
			
		||||
    return target_class(
 | 
			
		||||
        name=type(ex).__name__,
 | 
			
		||||
        title=title,
 | 
			
		||||
        description=str(ex),
 | 
			
		||||
        extra_fields=getattr(ex, 'extra_fields', {}))
 | 
			
		||||
@ -42,7 +43,11 @@ def _on_processing_error(ex):
 | 
			
		||||
 | 
			
		||||
def _on_stale_data_error(_ex):
 | 
			
		||||
    raise rest.errors.HttpConflict(
 | 
			
		||||
        'Someone else modified this in the meantime. Please try again.')
 | 
			
		||||
        name='IntegrityError',
 | 
			
		||||
        title='Integrity violation',
 | 
			
		||||
        description=(
 | 
			
		||||
            'Someone else modified this in the meantime. '
 | 
			
		||||
            'Please try again.'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate_config():
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@ def verify_version(entity, context, field_name='version'):
 | 
			
		||||
    actual_version = context.get_param_as_int(field_name, required=True)
 | 
			
		||||
    expected_version = entity.version
 | 
			
		||||
    if actual_version != expected_version:
 | 
			
		||||
        raise errors.InvalidParameterError(
 | 
			
		||||
        raise errors.IntegrityError(
 | 
			
		||||
            'Someone else modified this in the meantime. ' +
 | 
			
		||||
            'Please try again.')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -103,6 +103,7 @@ def application(env, start_response):
 | 
			
		||||
            '%d %s' % (ex.code, ex.reason),
 | 
			
		||||
            [('content-type', 'application/json')])
 | 
			
		||||
        blob = {
 | 
			
		||||
            'name': ex.name,
 | 
			
		||||
            'title': ex.title,
 | 
			
		||||
            'description': ex.description,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -5,10 +5,15 @@ class BaseHttpError(RuntimeError):
 | 
			
		||||
    code = None
 | 
			
		||||
    reason = None
 | 
			
		||||
 | 
			
		||||
    def __init__(self, description, title=None, extra_fields=None):
 | 
			
		||||
    def __init__(self, name, description, title=None, extra_fields=None):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
        # error name for programmers
 | 
			
		||||
        self.name = name
 | 
			
		||||
        # error description for humans
 | 
			
		||||
        self.description = description
 | 
			
		||||
        # short title for humans
 | 
			
		||||
        self.title = title or self.reason
 | 
			
		||||
        # additional fields for programmers
 | 
			
		||||
        self.extra_fields = extra_fields
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user