aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru
diff options
context:
space:
mode:
authorrr-2016-09-10 10:14:40 +0200
committerrr-2016-09-10 10:16:14 +0200
commit0a19e7bbd08c70b2728b127570cd032a00e8d244 (patch)
tree6b7f2d9c7631a08482cae004daaae18baf3fc08c /server/szurubooru
parentc516030c66cf7cece87bdca49da0e21c427fcb71 (diff)
server/errors: allow extra info in errors
Diffstat (limited to 'server/szurubooru')
-rw-r--r--server/szurubooru/errors.py20
-rw-r--r--server/szurubooru/facade.py25
-rw-r--r--server/szurubooru/rest/app.py7
-rw-r--r--server/szurubooru/rest/errors.py3
4 files changed, 33 insertions, 22 deletions
diff --git a/server/szurubooru/errors.py b/server/szurubooru/errors.py
index bd8c669..33bdfce 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 60e58d5..cead9a2 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 199d098..da8f2dd 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 f9755a7..0189147 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):

© 2015 - 2026 Jakob L. Kreuze