Fixed exception chain

This commit is contained in:
CocoByte 2024-05-08 16:04:16 -06:00
parent 24ec18c439
commit 5b8cf13eb1
No known key found for this signature in database
GPG key ID: BBFAA2526384C97F

View file

@ -762,14 +762,14 @@ class DomainAddUserView(DomainFormBaseView):
"requestor_email": requestor_email, "requestor_email": requestor_email,
}, },
) )
except EmailSendingError: except EmailSendingError as exc:
messages.warning(self.request, "Could not send email invitation.")
logger.warn( logger.warn(
"Could not sent email invitation to %s for domain %s", "Could not sent email invitation to %s for domain %s",
email, email,
self.object, self.object,
exc_info=True, exc_info=True,
) )
raise EmailSendingError("Could not send email invitation.") from exc
else: else:
if add_success: if add_success:
messages.success(self.request, f"{email} has been invited to this domain.") messages.success(self.request, f"{email} has been invited to this domain.")
@ -777,21 +777,21 @@ class DomainAddUserView(DomainFormBaseView):
def _make_invitation(self, email_address: str, requestor: User): def _make_invitation(self, email_address: str, requestor: User):
"""Make a Domain invitation for this email and redirect with a message.""" """Make a Domain invitation for this email and redirect with a message."""
# Check to see if an invite has already been sent (NOTE: we do not want to create an invite just yet.) # Check to see if an invite has already been sent (NOTE: we do not want to create an invite just yet.)
invite_exists = DomainInvitation.objects.get(email=email_address, domain=self.object) try:
if invite_exists: invite = DomainInvitation.objects.get(email=email_address, domain=self.object)
# that invitation already existed # that invitation already existed
messages.warning( messages.warning(
self.request, self.request,
f"{email_address} has already been invited to this domain.", f"{email_address} has already been invited to this domain.",
) )
else: except DomainInvitation.DoesNotExist:
#Try to send the invitation. If it succeeds, add it to the DomainInvitation table. #Try to send the invitation. If it succeeds, add it to the DomainInvitation table.
try: try:
self._send_domain_invitation_email(email=email_address, requestor=requestor) self._send_domain_invitation_email(email=email_address, requestor=requestor)
except Exception as exc: except EmailSendingError as exc:
raise EmailSendingError("Could not send SES email.") from exc messages.warning(self.request, "Could not send email invitation.")
else: else:
#(NOTE: if the invitation fails to send, no invitation should be added to the DomainInvitation table) #(NOTE: only create a domainInvitation if the e-mail sends correctly)
DomainInvitation.objects.get_or_create(email=email_address, domain=self.object) DomainInvitation.objects.get_or_create(email=email_address, domain=self.object)
return redirect(self.get_success_url()) return redirect(self.get_success_url())
@ -807,7 +807,10 @@ class DomainAddUserView(DomainFormBaseView):
return self._make_invitation(requested_email, requestor) return self._make_invitation(requested_email, requestor)
else: else:
# if user already exists then just send an email # if user already exists then just send an email
try:
self._send_domain_invitation_email(requested_email, requestor, add_success=False) self._send_domain_invitation_email(requested_email, requestor, add_success=False)
except Exception as exc:
messages.warning(self.request, "Could not send email invitation.")
try: try:
UserDomainRole.objects.create( UserDomainRole.objects.create(