diff --git a/server/szurubooru/api/user_api.py b/server/szurubooru/api/user_api.py index 8c70d8e..8bb6ac8 100644 --- a/server/szurubooru/api/user_api.py +++ b/server/szurubooru/api/user_api.py @@ -1,8 +1,8 @@ -''' Users public API. ''' +''' Exports UserListApi and UserDetailApi. ''' import sqlalchemy 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 def _serialize_user(authenticated_user, user): @@ -70,6 +70,8 @@ class UserDetailApi(BaseApi): ''' Retrieves an user. ''' self._auth_service.verify_privilege(context.user, 'users:view') 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)} def put(self, request, context, user_name): diff --git a/server/szurubooru/app.py b/server/szurubooru/app.py index 03b91db..520aaa1 100644 --- a/server/szurubooru/app.py +++ b/server/szurubooru/app.py @@ -29,16 +29,21 @@ class _CustomRequest(falcon.Request): raise falcon.HTTPMissingParam(name) 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): - raise falcon.HTTPBadRequest('Validation error', str(ex)) + raise falcon.HTTPBadRequest(title='Validation error', description=str(ex)) 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): - 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(): ''' 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.ValidationError, _on_validation_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('/user/{user_name}', user) diff --git a/server/szurubooru/errors.py b/server/szurubooru/errors.py index f2bec16..fa42ab2 100644 --- a/server/szurubooru/errors.py +++ b/server/szurubooru/errors.py @@ -11,3 +11,6 @@ class ValidationError(RuntimeError): class SearchError(RuntimeError): ''' 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. '''