Skip to content

Commit

Permalink
Merge branch 'dev' into 'main'
Browse files Browse the repository at this point in the history
chore: Handle store_reply_record error

See merge request locker/maily!20
  • Loading branch information
khaitranquang committed Jun 3, 2024
2 parents 910bc9a + 2f4cb5a commit ae30173
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/maily/aws/ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from maily.aws import AWS
from maily.logger import logger
from email.mime.text import MIMEText
from botocore.exceptions import ClientError
from botocore.exceptions import ClientError, ConnectionClosedError
from email.mime.multipart import MIMEMultipart
from maily.locker_api import store_reply_record
from email.mime.application import MIMEApplication
Expand Down Expand Up @@ -93,7 +93,9 @@ def ses_send_raw_email(self, from_address, to_address, subject, message_body, at
# ConfigurationSetName=AWS_SES_CONFIG_SET,
# )

store_reply_record(mail, ses_response)
stored = store_reply_record(mail, ses_response)
if not stored:
logger.error(f"[!] Store reply record error. Please check the Locker Reply API")
except ClientError as e:
# Handel SES HTTP error: https://docs.aws.amazon.com/ses/latest/APIReference-V2/CommonErrors.html
error_code = e.response.get("Error", {}).get("Code")
Expand All @@ -103,7 +105,7 @@ def ses_send_raw_email(self, from_address, to_address, subject, message_body, at
# logger.error(
# f'from_address: {from_address}\nto_address: {to_address}\ndata: {msg_with_attachments.as_string()}')
return False
except botocore.exceptions.ConnectionClosedError:
except ConnectionClosedError:
return None
return True

Expand Down
11 changes: 9 additions & 2 deletions src/maily/locker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ def store_reply_record(mail, ses_response):
payload = {"lookup": lookup, "encrypted_metadata": encrypted_metadata}
# Request to API to store payload
url = f'{ROOT_API}reply'
r = requests.post(url=url, json=payload, headers=HEADERS)

# Handle store reply record failed
retry = 0
while retry <= 5:
try:
requests.post(url=url, json=payload, headers=HEADERS)
return True
except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout, KeyError):
retry += 1
return False

def reply_allowed(from_address, to_address):
"""
Expand Down

0 comments on commit ae30173

Please sign in to comment.