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

@ -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)