From af4ad19169e846449d162aebe8a2fd6acd628a8e Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 22 Jun 2020 17:21:46 +0300 Subject: [PATCH] imap: add chain_err_summary error descriptions --- melib/src/backends/imap.rs | 41 ++++++++++++++++++---- melib/src/backends/imap/protocol_parser.rs | 7 ++-- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/melib/src/backends/imap.rs b/melib/src/backends/imap.rs index 722691d8..4ca34fe1 100644 --- a/melib/src/backends/imap.rs +++ b/melib/src/backends/imap.rs @@ -44,7 +44,7 @@ use crate::backends::{AccountHash, MailboxHash}; use crate::backends::{BackendMailbox, MailBackend, Mailbox, RefreshEventConsumer}; use crate::conf::AccountSettings; use crate::email::*; -use crate::error::{MeliError, Result}; +use crate::error::{MeliError, Result, ResultIntoMeliError}; use std::collections::{hash_map::DefaultHasher, BTreeMap}; use std::collections::{HashMap, HashSet}; use std::hash::Hasher; @@ -251,9 +251,15 @@ impl MailBackend for ImapType { return Ok(HashSet::default()); }; let cached_envs: (cache::MaxUID, Vec<(UID, Envelope)>); - cache::save_envelopes(uid_store.account_hash, mailbox_hash, *v, &[])?; + cache::save_envelopes(uid_store.account_hash, mailbox_hash, *v, &[]) + .chain_err_summary(|| { + "Could not save envelopes in cache in get()" + })?; cached_envs = - cache::get_envelopes(uid_store.account_hash, mailbox_hash, *v)?; + cache::get_envelopes(uid_store.account_hash, mailbox_hash, *v) + .chain_err_summary(|| { + "Could not get envelopes in cache in get()" + })?; let (_max_uid, envelopes) = debug!(cached_envs); *max_uid = _max_uid; let ret = envelopes.iter().map(|(_, env)| env.hash()).collect(); @@ -290,8 +296,17 @@ impl MailBackend for ImapType { /* first SELECT the mailbox to get READ/WRITE permissions (because EXAMINE only * returns READ-ONLY for both cases) */ - conn.select_mailbox(mailbox_hash, &mut response)?; - let examine_response = protocol_parser::select_response(&response)?; + conn.select_mailbox(mailbox_hash, &mut response) + .chain_err_summary(|| { + format!("Could not select mailbox {}", mailbox_path) + })?; + let examine_response = protocol_parser::select_response(&response) + .chain_err_summary(|| { + format!( + "Could not parse select response for mailbox {}", + mailbox_path + ) + })?; *can_create_flags.lock().unwrap() = examine_response.can_create_flags; debug!( "mailbox: {} examine_response: {:?}", @@ -347,7 +362,13 @@ impl MailBackend for ImapType { .as_bytes(), )? }; - conn.read_response(&mut response, RequiredResponses::FETCH_REQUIRED)?; + conn.read_response(&mut response, RequiredResponses::FETCH_REQUIRED) + .chain_err_summary(|| { + format!( + "Could not parse fetch response for mailbox {}", + mailbox_path + ) + })?; debug!( "fetch response is {} bytes and {} lines", response.len(), @@ -407,7 +428,13 @@ impl MailBackend for ImapType { .iter() .map(|(uid, env)| (*uid, env)) .collect::>(), - )?; + ) + .chain_err_summary(|| { + format!( + "Could not save envelopes in cache for mailbox {}", + mailbox_path + ) + })?; } for &env_hash in cached_hash_set.difference(&valid_hash_set) { conn.add_refresh_event(RefreshEvent { diff --git a/melib/src/backends/imap/protocol_parser.rs b/melib/src/backends/imap/protocol_parser.rs index 2575c1ec..4e64e9ab 100644 --- a/melib/src/backends/imap/protocol_parser.rs +++ b/melib/src/backends/imap/protocol_parser.rs @@ -21,6 +21,7 @@ use super::*; use crate::email::parser::{BytesExt, IResult}; +use crate::error::ResultIntoMeliError; use crate::get_path_hash; use nom::{ branch::{alt, permutation}, @@ -242,8 +243,10 @@ impl Into> for ImapResponse { Self::No(ResponseCode::Alert(msg)) | Self::Bad(ResponseCode::Alert(msg)) => { Err(MeliError::new(msg)) } - Self::No(_) => Err(MeliError::new("IMAP NO Response.")), - Self::Bad(_) => Err(MeliError::new("IMAP BAD Response.")), + Self::No(err) => Err(MeliError::new(format!("{:?}", err))) + .chain_err_summary(|| "IMAP NO Response.".to_string()), + Self::Bad(err) => Err(MeliError::new(format!("{:?}", err))) + .chain_err_summary(|| "IMAP BAD Response.".to_string()), } } }