server/users: fix returning invalid users from api
This commit is contained in:
parent
a157d2db0e
commit
8bdcb04665
3 changed files with 17 additions and 6 deletions
|
@ -1,8 +1,8 @@
|
||||||
''' Users public API. '''
|
''' Exports UserListApi and UserDetailApi. '''
|
||||||
|
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from szurubooru.api.base_api import BaseApi
|
from szurubooru.api.base_api import BaseApi
|
||||||
from szurubooru.errors import IntegrityError, ValidationError
|
from szurubooru.errors import IntegrityError, ValidationError, NotFoundError
|
||||||
from szurubooru.services.search import UserSearchConfig, SearchExecutor
|
from szurubooru.services.search import UserSearchConfig, SearchExecutor
|
||||||
|
|
||||||
def _serialize_user(authenticated_user, user):
|
def _serialize_user(authenticated_user, user):
|
||||||
|
@ -70,6 +70,8 @@ class UserDetailApi(BaseApi):
|
||||||
''' Retrieves an user. '''
|
''' Retrieves an user. '''
|
||||||
self._auth_service.verify_privilege(context.user, 'users:view')
|
self._auth_service.verify_privilege(context.user, 'users:view')
|
||||||
user = self._user_service.get_by_name(context.session, user_name)
|
user = self._user_service.get_by_name(context.session, user_name)
|
||||||
|
if not user:
|
||||||
|
raise NotFoundError('User %r not found.' % user_name)
|
||||||
return {'user': _serialize_user(context.user, user)}
|
return {'user': _serialize_user(context.user, user)}
|
||||||
|
|
||||||
def put(self, request, context, user_name):
|
def put(self, request, context, user_name):
|
||||||
|
|
|
@ -29,16 +29,21 @@ class _CustomRequest(falcon.Request):
|
||||||
raise falcon.HTTPMissingParam(name)
|
raise falcon.HTTPMissingParam(name)
|
||||||
|
|
||||||
def _on_auth_error(ex, request, response, params):
|
def _on_auth_error(ex, request, response, params):
|
||||||
raise falcon.HTTPForbidden('Authentication error', str(ex))
|
raise falcon.HTTPForbidden(
|
||||||
|
title='Authentication error', description=str(ex))
|
||||||
|
|
||||||
def _on_validation_error(ex, request, response, params):
|
def _on_validation_error(ex, request, response, params):
|
||||||
raise falcon.HTTPBadRequest('Validation error', str(ex))
|
raise falcon.HTTPBadRequest(title='Validation error', description=str(ex))
|
||||||
|
|
||||||
def _on_search_error(ex, request, response, params):
|
def _on_search_error(ex, request, response, params):
|
||||||
raise falcon.HTTPBadRequest('Search error', str(ex))
|
raise falcon.HTTPBadRequest(title='Search error', description=str(ex))
|
||||||
|
|
||||||
def _on_integrity_error(ex, request, response, params):
|
def _on_integrity_error(ex, request, response, params):
|
||||||
raise falcon.HTTPConflict('Integrity violation', ex.args[0])
|
raise falcon.HTTPConflict(
|
||||||
|
title='Integrity violation', description=ex.args[0])
|
||||||
|
|
||||||
|
def _on_not_found_error(ex, request, response, params):
|
||||||
|
raise falcon.HTTPNotFound(title='Not found', description=str(ex))
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
''' Creates a WSGI compatible App object. '''
|
''' Creates a WSGI compatible App object. '''
|
||||||
|
@ -78,6 +83,7 @@ def create_app():
|
||||||
app.add_error_handler(szurubooru.errors.IntegrityError, _on_integrity_error)
|
app.add_error_handler(szurubooru.errors.IntegrityError, _on_integrity_error)
|
||||||
app.add_error_handler(szurubooru.errors.ValidationError, _on_validation_error)
|
app.add_error_handler(szurubooru.errors.ValidationError, _on_validation_error)
|
||||||
app.add_error_handler(szurubooru.errors.SearchError, _on_search_error)
|
app.add_error_handler(szurubooru.errors.SearchError, _on_search_error)
|
||||||
|
app.add_error_handler(szurubooru.errors.NotFoundError, _on_not_found_error)
|
||||||
|
|
||||||
app.add_route('/users/', user_list)
|
app.add_route('/users/', user_list)
|
||||||
app.add_route('/user/{user_name}', user)
|
app.add_route('/user/{user_name}', user)
|
||||||
|
|
|
@ -11,3 +11,6 @@ class ValidationError(RuntimeError):
|
||||||
|
|
||||||
class SearchError(RuntimeError):
|
class SearchError(RuntimeError):
|
||||||
''' Search error (e.g. trying to use special: where it doesn't make sense) '''
|
''' Search error (e.g. trying to use special: where it doesn't make sense) '''
|
||||||
|
|
||||||
|
class NotFoundError(RuntimeError):
|
||||||
|
''' Error thrown when a resource (usually DB) couldn't be found. '''
|
||||||
|
|
Loading…
Reference in a new issue