From 93cd7de0e0e31ba7ec72098d8eda71be93a64e76 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 28 Oct 2014 12:10:44 -0400 Subject: [PATCH] Handle email errors in a better manner --- endpoints/common.py | 6 ++++++ static/js/app.js | 4 ++-- util/useremails.py | 12 +++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/endpoints/common.py b/endpoints/common.py index bc7498738..bd844992d 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -20,6 +20,7 @@ from functools import wraps from config import getFrontendVisibleConfig from external_libraries import get_external_javascript, get_external_css from endpoints.notificationhelper import spawn_notification +from util.useremails import CannotSendEmailException import features @@ -129,6 +130,11 @@ def handle_dme(ex): logger.exception(ex) return make_response(json.dumps({'message': ex.message}), 400) +@app.errorhandler(CannotSendEmailException) +def handle_emailexception(ex): + logger.exception(ex) + message = 'Could not send email. Please contact an administrator and report this problem.' + return make_response(json.dumps({'message': message}), 400) def random_string(): random = SystemRandom() diff --git a/static/js/app.js b/static/js/app.js index aae470c32..f751cdd98 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -2644,9 +2644,9 @@ quayApp.directive('userSetup', function () { $scope.errorMessage = ''; $scope.sent = true; $scope.sendingRecovery = false; - }, function(result) { + }, function(resp) { $scope.invalidRecovery = true; - $scope.errorMessage = result.data; + $scope.errorMessage = ApiService.getErrorMessage(resp, 'Cannot send recovery email'); $scope.sent = false; $scope.sendingRecovery = false; }); diff --git a/util/useremails.py b/util/useremails.py index 507af1724..5a217fbbe 100644 --- a/util/useremails.py +++ b/util/useremails.py @@ -1,3 +1,6 @@ +import logging +import traceback + from flask.ext.mail import Message from app import mail, app, get_app_url @@ -5,8 +8,12 @@ from data import model from util.gravatar import compute_hash from util.jinjautil import get_template_env +logger = logging.getLogger(__name__) template_env = get_template_env("emails") +class CannotSendEmailException(Exception): + pass + def send_email(recipient, subject, template_file, parameters): app_title = app.config['REGISTRY_TITLE_SHORT'] app_url = get_app_url() @@ -30,8 +37,11 @@ def send_email(recipient, subject, template_file, parameters): msg = Message('[%s] %s' % (app_title, subject), recipients=[recipient]) msg.html = rendered_html - mail.send(msg) + try: + mail.send(msg) + except Exception as ex: + raise CannotSendEmailException(ex.message) def send_password_changed(username, email): send_email(email, 'Account password changed', 'passwordchanged', {