From 735ca07dee4d39885613f7c31456c04bf6b855b0 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 10 Mar 2016 14:56:47 +0000 Subject: [PATCH] Handle HTTPError from API and routing errors from flask. Removed catching the HTTPError where applicable. --- app/__init__.py | 30 +++++++++++++++++++++++++++--- app/main/dao/templates_dao.py | 5 +---- app/main/dao/users_dao.py | 5 ----- app/main/views/dashboard.py | 1 - app/main/views/jobs.py | 1 - 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index dfebc8dd0..67e53be0a 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -7,10 +7,11 @@ from flask._compat import string_types from flask_login import LoginManager from flask_wtf import CsrfProtect from notifications_python_client import HTTPError -from werkzeug.exceptions import abort from pygments import highlight from pygments.lexers import JavascriptLexer from pygments.formatters import HtmlFormatter +from werkzeug.exceptions import abort + from app.notify_client.api_client import NotificationsAdminAPIClient from app.notify_client.api_key_api_client import ApiKeyApiClient from app.notify_client.user_api_client import UserApiClient @@ -177,10 +178,33 @@ def useful_headers_after_request(response): def register_errorhandlers(application): + def _error_response(error_code): + resp = make_response(render_template("error/{0}.html".format(error_code)), error_code) + return useful_headers_after_request(resp) + @application.errorhandler(HTTPError) def render_http_error(error): error_code = getattr(error, 'code', 500) if error_code not in [401, 404, 403, 500]: error_code = 500 - resp = make_response(render_template("error/{0}.html".format(error_code)), error_code) - return useful_headers_after_request(resp) + return _error_response(error_code) + + @application.errorhandler(400) + def handle_bad_request(error): + return _error_response(404) + + @application.errorhandler(404) + def handle_not_found(error): + return _error_response(404) + + @application.errorhandler(403) + def handle_not_authorized(error): + return _error_response(403) + + @application.errorhandler(401) + def handle_no_permissions(error): + return _error_response(401) + + @application.errorhandler(Exception) + def handle_bad_request(error): + return _error_response(500) diff --git a/app/main/dao/templates_dao.py b/app/main/dao/templates_dao.py index 69bbd272f..d75a6c998 100644 --- a/app/main/dao/templates_dao.py +++ b/app/main/dao/templates_dao.py @@ -19,10 +19,7 @@ def get_service_templates(service_id): def get_service_template_or_404(service_id, template_id): - try: - return notifications_api_client.get_service_template(service_id, template_id) - except HTTPError as e: - abort(e.status_code) + return notifications_api_client.get_service_template(service_id, template_id) def delete_service_template(service_id, template_id): diff --git a/app/main/dao/users_dao.py b/app/main/dao/users_dao.py index 932a98cd3..9b27d5839 100644 --- a/app/main/dao/users_dao.py +++ b/app/main/dao/users_dao.py @@ -1,11 +1,6 @@ -from datetime import datetime from notifications_python_client import HTTPError -from sqlalchemy.orm import load_only - from app import login_manager -from app.main.encryption import hashpw - from app import user_api_client # diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 415a7c141..e43f66b15 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -9,7 +9,6 @@ from flask_login import login_required from app.main import main from app.main.dao.services_dao import get_service_by_id from app.main.dao import templates_dao -from notifications_python_client.errors import HTTPError from app import job_api_client diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index 6d2982a73..9847ef2cc 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -8,7 +8,6 @@ from flask import ( jsonify ) from flask_login import login_required -from notifications_python_client.errors import HTTPError from utils.template import Template from app import job_api_client, notification_api_client