From b465a34e8f056b083db9f84c96b4695ffbfeb1ec Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 11 Apr 2017 23:02:26 -0400 Subject: [PATCH] Temporary fix for additional exception classes handled in Flask The recent change to Flask-restful broke the other registered exception handlers, so this temporarily handles the decorated cases as well, until we put in place a proper registration model for Flask and Flask-restful handled exceptions --- endpoints/api/__init__.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/endpoints/api/__init__.py b/endpoints/api/__init__.py index becc1be3a..f434d1fd5 100644 --- a/endpoints/api/__init__.py +++ b/endpoints/api/__init__.py @@ -24,9 +24,13 @@ from endpoints.csrf import csrf_protect from endpoints.exception import (ApiException, Unauthorized, InvalidRequest, InvalidResponse, FreshLoginRequired, NotFound) from endpoints.decorators import check_anon_protection +from endpoints.decorated import (handle_dme, handle_emailexception, handle_configexception, + handle_too_many_login_attempts) +from util.config.provider.baseprovider import CannotWriteConfigException from util.metrics.metricqueue import time_decorator from util.names import parse_namespace_repository from util.pagination import encrypt_page_token, decrypt_page_token +from util.useremails import CannotSendEmailException logger = logging.getLogger(__name__) api_bp = Blueprint('api', __name__) @@ -35,6 +39,20 @@ 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')