From 3ea330c884bbf891f20f8dd70ace76ea8105b178 Mon Sep 17 00:00:00 2001 From: rustmailer Date: Thu, 14 May 2026 20:25:56 +0800 Subject: [PATCH] update --- crates/core/src/account/state.rs | 14 +++++++----- crates/core/src/cache/imap/download/mod.rs | 25 +++++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/crates/core/src/account/state.rs b/crates/core/src/account/state.rs index ab3d53a..14089dc 100644 --- a/crates/core/src/account/state.rs +++ b/crates/core/src/account/state.rs @@ -239,11 +239,15 @@ impl DownloadState { pub fn append_session_error(account_id: u64, error: String) -> BichonResult<()> { Self::update_state(account_id, move |current| { let mut updated = current.clone(); - if let Some(ref mut session) = updated.active_session { - let new_error = AccountError { - error, - at: utc_now!(), - }; + let new_error = AccountError { + error, + at: utc_now!(), + }; + let target = updated + .active_session + .as_mut() + .or_else(|| updated.history.last_mut()); + if let Some(session) = target { session.errors.push(new_error); let to_remove = session.errors.len().saturating_sub(30); if to_remove > 0 { diff --git a/crates/core/src/cache/imap/download/mod.rs b/crates/core/src/cache/imap/download/mod.rs index 567b1ce..c4653f9 100644 --- a/crates/core/src/cache/imap/download/mod.rs +++ b/crates/core/src/cache/imap/download/mod.rs @@ -53,10 +53,12 @@ pub async fn process_imap_download( let mut session = match ImapExecutor::create_connection(account_id).await { Ok(session) => session, Err(e) => { + let err_msg = format!("Failed to connect to IMAP server: {:#?}", e); + DownloadState::append_session_error(account_id, err_msg.clone())?; DownloadState::update_session_status( account_id, DownloadStatus::Failed, - Some(format!("Failed to connect to IMAP server: {}", e)), + Some(err_msg), )?; return Err(e); } @@ -64,8 +66,9 @@ pub async fn process_imap_download( let remote_mailboxes = match get_download_folders(account, &mut session).await { Ok(mailboxes) => mailboxes, Err(err) => { - let err_msg = format!("Failed to fetch mailboxes: {}", err); + let err_msg = format!("Failed to fetch mailboxes: {:#?}", err); warn!(account_id = account.id, error = %err, "{}", err_msg); + DownloadState::append_session_error(account_id, err_msg.clone())?; DownloadState::update_session_status( account_id, DownloadStatus::Failed, @@ -106,10 +109,12 @@ pub async fn process_imap_download( DownloadState::update_session_status(account_id, DownloadStatus::Success, None)?; } Err(e) => { + let err_msg = format!("Email Download interrupted: {:#?}", e); + DownloadState::append_session_error(account_id, err_msg.clone())?; DownloadState::update_session_status( account_id, DownloadStatus::Failed, - Some(format!("Email Download interrupted: {:#?}", e)), + Some(err_msg), )?; } } @@ -119,11 +124,15 @@ pub async fn process_imap_download( let local_mailboxes = MailBox::list_all(account_id)?; match reconcile_mailboxes(account, &remote_mailboxes, &local_mailboxes, token).await { Ok(_) => DownloadState::update_session_status(account_id, DownloadStatus::Success, None)?, - Err(e) => DownloadState::update_session_status( - account_id, - DownloadStatus::Failed, - Some(format!("Email Download interrupted: {:#?}", e)), - )?, + Err(e) => { + let err_msg = format!("Email Download interrupted: {:#?}", e); + DownloadState::append_session_error(account_id, err_msg.clone())?; + DownloadState::update_session_status( + account_id, + DownloadStatus::Failed, + Some(err_msg), + )?; + } } let elapsed_time = start_time.elapsed().as_secs(); debug!(