From fe7036dae362ae4bce2cfe6932a94dacd7a2dce9 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Mon, 12 Mar 2018 23:27:40 -0700 Subject: [PATCH] Proper exception handling for email send error --- todo/utils.py | 4 ++-- todo/views.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/todo/utils.py b/todo/utils.py index e66f514..cb7efb7 100644 --- a/todo/utils.py +++ b/todo/utils.py @@ -41,5 +41,5 @@ def send_notify_mail(request, new_task): send_mail( email_subject, email_body, new_task.created_by.email, [new_task.assigned_to.email], fail_silently=False) - except ConnectionRefusedError: - messages.error(request, "Task saved but mail not sent. Contact your administrator.") + except Exception as e: + messages.error(request, "Task saved but mail not sent. Contact your administrator.: {}".format(e)) diff --git a/todo/views.py b/todo/views.py index 2c839eb..1f4e2a7 100644 --- a/todo/views.py +++ b/todo/views.py @@ -178,15 +178,16 @@ def task_detail(request, task_id): # Get list of all thread participants - everyone who has commented on it plus task creator. commenters = Comment.objects.filter(task=task) - recip_list = [c.author.email for c in commenters] + recip_list = [ca.author.email for ca in commenters] recip_list.append(task.created_by.email) recip_list = list(set(recip_list)) # Eliminate duplicates try: send_mail(email_subject, email_body, task.created_by.email, recip_list, fail_silently=False) messages.success(request, "Comment sent to thread participants.") - except ConnectionRefusedError: - messages.error(request, "Comment saved but mail not sent. Contact your administrator.") + except Exception as e: + messages.error( + request, "Comment saved but mail not sent. Contact your administrator. :: {}".format(e)) messages.success(request, "The task has been edited.")