Unify app and api exception handling

Move some confi to an immutable section
Make ApiExceptions real werkzeug exceptions
This commit is contained in:
Jake Moshenko 2017-04-14 11:18:01 -04:00
parent 218acaffbf
commit 8d279c8cc4
3 changed files with 47 additions and 59 deletions

View file

@ -1,6 +1,7 @@
from enum import Enum
from flask import url_for
from werkzeug.exceptions import HTTPException
from auth.auth_context import get_authenticated_user
@ -32,7 +33,7 @@ ERROR_DESCRIPTION = {
}
class ApiException(Exception):
class ApiException(HTTPException):
"""
Represents an error in the application/problem+json format.
@ -58,9 +59,12 @@ class ApiException(Exception):
def __init__(self, error_type, status_code, error_description, payload=None):
Exception.__init__(self)
self.error_description = error_description
self.status_code = status_code
self.code = status_code
self.payload = payload
self.error_type = error_type
self.data = self.to_dict()
super(ApiException, self).__init__(error_description, None)
def to_dict(self):
rv = dict(self.payload or ())
@ -72,7 +76,7 @@ class ApiException(Exception):
rv['error_type'] = self.error_type.value # TODO: deprecate
rv['title'] = self.error_type.value
rv['type'] = url_for('api.error', error_type=self.error_type.value, _external=True)
rv['status'] = self.status_code
rv['status'] = self.code
return rv