Fail script on all server errors

Remove local exception handling for IMAP operations to ensure script
exits on any server failure (e.g., auth, connection, search errors).
This allows systemd OnFailure to notify on fixable issues.

Changes:
- Remove try-except in check_duplicate, sync_folder, sync_all_folders
- Let IMAP exceptions propagate to main() for exit(1)
This commit is contained in:
Jeena 2026-01-04 20:10:33 +09:00
parent 69ec294a56
commit 05f2e6c870

View file

@ -104,13 +104,9 @@ class DestImap(ImapClient):
def check_duplicate(self, msg_id: str):
"""Check if Message-ID exists in current folder."""
try:
results = self._search(f'HEADER Message-ID "{msg_id}"')
logging.debug(f"Check duplicate for {msg_id}: found {len(results)} matches")
return len(results) > 0
except Exception as e:
logging.error(f"Error checking duplicate for {msg_id}: {e}")
return False
results = self._search(f'HEADER Message-ID "{msg_id}"')
logging.debug(f"Check duplicate for {msg_id}: found {len(results)} matches")
return len(results) > 0
def _create_folder(self, folder: str):
"""Private: Create a folder."""
@ -179,15 +175,12 @@ class EmailForwarder:
total_forwarded = 0
for folder in folders:
try:
uids = self.source.get_new_emails(
folder, self.last_run.strftime("%d-%b-%Y")
)
logging.info(f"Found {len(uids)} emails in {folder}")
forwarded = self.sync_folder(folder, uids)
total_forwarded += forwarded
except Exception as e:
logging.error(f"Error syncing {folder}: {e}")
uids = self.source.get_new_emails(
folder, self.last_run.strftime("%d-%b-%Y")
)
logging.info(f"Found {len(uids)} emails in {folder}")
forwarded = self.sync_folder(folder, uids)
total_forwarded += forwarded
self.source._disconnect()
self.dest._disconnect()
@ -198,34 +191,27 @@ class EmailForwarder:
forwarded = 0
for uid in uids:
uid_str = uid.decode()
try:
raw_msg = self.source._fetch(uid_str)
raw_msg = self.source._fetch(uid_str)
msg = message_from_bytes(raw_msg)
msg_id = msg.get("Message-ID")
if not msg_id:
continue
if msg_id in self.processed_ids:
continue
self.dest.ensure_folder_exists(folder)
if self.dest.check_duplicate(msg_id):
continue
if msg_id in self.processed_ids:
continue
if self.dest.check_duplicate(msg_id):
continue
msg = message_from_bytes(raw_msg)
msg_id = msg.get("Message-ID")
if not msg_id:
continue
if msg_id in self.processed_ids:
continue
self.dest.ensure_folder_exists(folder)
if self.dest.check_duplicate(msg_id):
continue
if not self.dry_run:
self.dest.append_email(folder, raw_msg)
self.processed_ids.add(msg_id)
with open(self.processed_file, "a") as f:
f.write(msg_id + "\n")
forwarded += 1
logging.info(f"Forwarded {msg_id} from {folder}")
else:
logging.info(f"Dry-run: Would forward {msg_id} from {folder}")
except Exception as e:
logging.error(f"Error processing UID {uid_str}: {e}")
if not self.dry_run:
self.dest.append_email(folder, raw_msg)
self.processed_ids.add(msg_id)
with open(self.processed_file, "a") as f:
f.write(msg_id + "\n")
forwarded += 1
logging.info(f"Forwarded {msg_id} from {folder}")
else:
logging.info(f"Dry-run: Would forward {msg_id} from {folder}")
return forwarded
def run(self):