Unify app and api exception handling
Move some confi to an immutable section Make ApiExceptions real werkzeug exceptions
This commit is contained in:
parent
218acaffbf
commit
8d279c8cc4
3 changed files with 47 additions and 59 deletions
|
@ -39,27 +39,6 @@ api_bp = Blueprint('api', __name__)
|
|||
class ApiExceptionHandlingApi(Api):
|
||||
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
|
||||
def handle_error(self, error):
|
||||
# TODO: Fix this into a proper error registration model that works in *both* Flask and
|
||||
# Flask-Restful!
|
||||
if isinstance(error, model.DataModelException):
|
||||
return handle_dme(error)
|
||||
|
||||
if isinstance(error, CannotSendEmailException):
|
||||
return handle_emailexception(error)
|
||||
|
||||
if isinstance(error, CannotWriteConfigException):
|
||||
return handle_configexception(error)
|
||||
|
||||
if isinstance(error, model.TooManyLoginAttemptsException):
|
||||
return handle_too_many_login_attempts(error)
|
||||
|
||||
if isinstance(error, ApiException):
|
||||
response = Response(json.dumps(error.to_dict()), error.status_code,
|
||||
mimetype='application/json')
|
||||
if error.status_code == 401:
|
||||
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
|
||||
(error.error_type.value, error.error_description))
|
||||
return response
|
||||
return super(ApiExceptionHandlingApi, self).handle_error(error)
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Reference in a new issue