diff --git a/server/szurubooru/errors.py b/server/szurubooru/errors.py index bd8c6691..33bdfce1 100644 --- a/server/szurubooru/errors.py +++ b/server/szurubooru/errors.py @@ -1,28 +1,34 @@ -class ConfigError(RuntimeError): +class BaseError(RuntimeError): + def __init__(self, message='Unknown error', extra_fields=None): + super().__init__(message) + self.extra_fields = extra_fields + + +class ConfigError(BaseError): pass -class AuthError(RuntimeError): +class AuthError(BaseError): pass -class IntegrityError(RuntimeError): +class IntegrityError(BaseError): pass -class ValidationError(RuntimeError): +class ValidationError(BaseError): pass -class SearchError(RuntimeError): +class SearchError(BaseError): pass -class NotFoundError(RuntimeError): +class NotFoundError(BaseError): pass -class ProcessingError(RuntimeError): +class ProcessingError(BaseError): pass diff --git a/server/szurubooru/facade.py b/server/szurubooru/facade.py index 60e58d5a..cead9a27 100644 --- a/server/szurubooru/facade.py +++ b/server/szurubooru/facade.py @@ -9,34 +9,35 @@ from szurubooru import config, errors, rest from szurubooru import api, middleware +def _map_error(ex, target_class, title): + return target_class( + title=title, + description=str(ex), + extra_fields=getattr(ex, 'extra_fields', {})) + + def _on_auth_error(ex): - raise rest.errors.HttpForbidden( - title='Authentication error', description=str(ex)) + raise _map_error(ex, rest.errors.HttpForbidden, 'Authentication error') def _on_validation_error(ex): - raise rest.errors.HttpBadRequest( - title='Validation error', description=str(ex)) + raise _map_error(ex, rest.errors.HttpBadRequest, 'Validation error') def _on_search_error(ex): - raise rest.errors.HttpBadRequest( - title='Search error', description=str(ex)) + raise _map_error(ex, rest.errors.HttpBadRequest, 'Search error') def _on_integrity_error(ex): - raise rest.errors.HttpConflict( - title='Integrity violation', description=ex.args[0]) + raise _map_error(ex, rest.errors.HttpConflict, 'Integrity violation') def _on_not_found_error(ex): - raise rest.errors.HttpNotFound( - title='Not found', description=str(ex)) + raise _map_error(ex, rest.errors.HttpNotFound, 'Not found') def _on_processing_error(ex): - raise rest.errors.HttpBadRequest( - title='Processing error', description=str(ex)) + raise _map_error(ex, rest.errors.HttpBadRequest, 'Processing error') def _on_stale_data_error(_ex): diff --git a/server/szurubooru/rest/app.py b/server/szurubooru/rest/app.py index 199d0980..da8f2dd9 100644 --- a/server/szurubooru/rest/app.py +++ b/server/szurubooru/rest/app.py @@ -102,7 +102,10 @@ def application(env, start_response): start_response( '%d %s' % (ex.code, ex.reason), [('content-type', 'application/json')]) - return (_dump_json({ + blob = { 'title': ex.title, 'description': ex.description, - }).encode('utf-8'),) + } + for key, value in ex.extra_fields.items(): + blob[key] = value + return (_dump_json(blob).encode('utf-8'),) diff --git a/server/szurubooru/rest/errors.py b/server/szurubooru/rest/errors.py index f9755a74..0189147f 100644 --- a/server/szurubooru/rest/errors.py +++ b/server/szurubooru/rest/errors.py @@ -5,10 +5,11 @@ class BaseHttpError(RuntimeError): code = None reason = None - def __init__(self, description, title=None): + def __init__(self, description, title=None, extra_fields=None): super().__init__() self.description = description self.title = title or self.reason + self.extra_fields = extra_fields class HttpBadRequest(BaseHttpError):