Skip to content

Commit 0937230

Browse files
committed
refactor: receive_imf: Don't abort processing message on non-critical errors
Follow-up to f27d54f. Non-critical errors shouldn't lead to missing messages. But in debug builds, panic on such errors, otherwise we won't catch them w/o special tests.
1 parent 5051240 commit 0937230

File tree

2 files changed

+82
-60
lines changed

2 files changed

+82
-60
lines changed

src/receive_imf.rs

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::key::self_fingerprint_opt;
2929
use crate::key::{DcKey, Fingerprint};
3030
use crate::log::LogExt;
3131
use crate::log::{info, warn};
32-
use crate::logged_debug_assert;
3332
use crate::message::{
3433
self, Message, MessageState, MessengerMessage, MsgId, Viewtype, rfc724_mid_exists,
3534
};
@@ -45,6 +44,7 @@ use crate::sync::Sync::*;
4544
use crate::tools::{self, buf_compress, remove_subject_prefix};
4645
use crate::{chatlist_events, ensure_and_debug_assert, ensure_and_debug_assert_eq, location};
4746
use crate::{contact, imap};
47+
use crate::{logged_debug_assert, logged_debug_assert_ok};
4848

4949
/// This is the struct that is returned after receiving one email (aka MIME message).
5050
///
@@ -1162,10 +1162,8 @@ async fn decide_chat_assignment(
11621162
let now = tools::time();
11631163
let update_config = if last_time.saturating_add(24 * 60 * 60) <= now {
11641164
let mut msg = Message::new_text(stock_str::cant_decrypt_outgoing_msgs(context).await);
1165-
chat::add_device_msg(context, None, Some(&mut msg))
1166-
.await
1167-
.log_err(context)
1168-
.ok();
1165+
let res = chat::add_device_msg(context, None, Some(&mut msg)).await;
1166+
logged_debug_assert_ok!(context, res).ok();
11691167
true
11701168
} else {
11711169
last_time > now
@@ -1880,7 +1878,7 @@ async fn add_parts(
18801878
};
18811879

18821880
for (group_changes_msg, cmd, added_removed_id) in group_changes.extra_msgs {
1883-
chat::add_info_msg_with_cmd(
1881+
let res = chat::add_info_msg_with_cmd(
18841882
context,
18851883
chat_id,
18861884
&group_changes_msg,
@@ -1891,7 +1889,8 @@ async fn add_parts(
18911889
None,
18921890
added_removed_id,
18931891
)
1894-
.await?;
1892+
.await;
1893+
logged_debug_assert_ok!(context, res).ok();
18951894
}
18961895

18971896
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
@@ -1947,7 +1946,7 @@ async fn add_parts(
19471946
if part.is_reaction {
19481947
let reaction_str = simplify::remove_footers(part.msg.as_str());
19491948
let is_incoming_fresh = mime_parser.incoming && !seen;
1950-
set_msg_reaction(
1949+
let res = set_msg_reaction(
19511950
context,
19521951
mime_in_reply_to,
19531952
chat_id,
@@ -1956,16 +1955,21 @@ async fn add_parts(
19561955
Reaction::from(reaction_str.as_str()),
19571956
is_incoming_fresh,
19581957
)
1959-
.await?;
1958+
.await;
1959+
if logged_debug_assert_ok!(context, res).is_err() {
1960+
continue;
1961+
}
19601962
}
19611963

19621964
let mut param = part.param.clone();
19631965
if is_system_message != SystemMessage::Unknown {
19641966
param.set_int(Param::Cmd, is_system_message as i32);
19651967
}
19661968

1967-
if let Some(replace_msg_id) = replace_msg_id {
1968-
let placeholder = Message::load_from_db(context, replace_msg_id).await?;
1969+
if let Some(placeholder) = match replace_msg_id {
1970+
None => None,
1971+
Some(replace_msg_id) => Message::load_from_db_optional(context, replace_msg_id).await?,
1972+
} {
19691973
for key in [
19701974
Param::WebxdcSummary,
19711975
Param::WebxdcSummaryTimestamp,
@@ -1976,6 +1980,8 @@ async fn add_parts(
19761980
param.set(key, value);
19771981
}
19781982
}
1983+
} else {
1984+
replace_msg_id = None;
19791985
}
19801986

19811987
let (msg, typ): (&str, Viewtype) = if let Some(better_msg) = &better_msg {
@@ -2113,26 +2119,29 @@ RETURNING id
21132119
if let Some(topic) = mime_parser.get_header(HeaderDef::IrohGossipTopic) {
21142120
// default encoding of topic ids is `hex`.
21152121
let mut topic_raw = [0u8; 32];
2116-
BASE32_NOPAD
2122+
let res = BASE32_NOPAD
21172123
.decode_mut(topic.to_ascii_uppercase().as_bytes(), &mut topic_raw)
21182124
.map_err(|e| e.error)
2119-
.context("Wrong gossip topic header")?;
2120-
2121-
let topic = TopicId::from_bytes(topic_raw);
2122-
insert_topic_stub(context, *msg_id, topic).await?;
2125+
.context("Wrong gossip topic header");
2126+
if logged_debug_assert_ok!(context, res).is_ok() {
2127+
let topic = TopicId::from_bytes(topic_raw);
2128+
let res = insert_topic_stub(context, *msg_id, topic).await;
2129+
logged_debug_assert_ok!(context, res).ok();
2130+
}
21232131
} else {
21242132
warn!(context, "webxdc doesn't have a gossip topic")
21252133
}
21262134
}
21272135

2128-
maybe_set_logging_xdc_inner(
2136+
let res = maybe_set_logging_xdc_inner(
21292137
context,
21302138
part.typ,
21312139
chat_id,
21322140
part.param.get(Param::Filename),
21332141
*msg_id,
21342142
)
2135-
.await?;
2143+
.await;
2144+
logged_debug_assert_ok!(context, res).ok();
21362145
}
21372146

21382147
if let Some(replace_msg_id) = replace_msg_id {
@@ -3645,21 +3654,21 @@ async fn add_or_lookup_contacts_by_address_list(
36453654
) -> Result<Vec<Option<ContactId>>> {
36463655
let mut contact_ids = Vec::new();
36473656
for info in address_list {
3657+
contact_ids.push(None);
36483658
let addr = &info.addr;
36493659
if !may_be_valid_addr(addr) {
3650-
contact_ids.push(None);
36513660
continue;
36523661
}
36533662
let display_name = info.display_name.as_deref();
3654-
if let Ok(addr) = ContactAddress::new(addr) {
3655-
let (contact_id, _) =
3656-
Contact::add_or_lookup(context, display_name.unwrap_or_default(), &addr, origin)
3657-
.await?;
3658-
contact_ids.push(Some(contact_id));
3659-
} else {
3663+
let Ok(addr) = ContactAddress::new(addr) else {
36603664
warn!(context, "Contact with address {:?} cannot exist.", addr);
3661-
contact_ids.push(None);
3662-
}
3665+
continue;
3666+
};
3667+
let res =
3668+
Contact::add_or_lookup(context, display_name.unwrap_or_default(), &addr, origin).await;
3669+
let contact_id = logged_debug_assert_ok!(context, res).ok().map(|(id, _)| id);
3670+
contact_ids.pop();
3671+
contact_ids.push(contact_id);
36633672
}
36643673

36653674
Ok(contact_ids)
@@ -3676,9 +3685,9 @@ async fn add_or_lookup_key_contacts(
36763685
let mut contact_ids = Vec::new();
36773686
let mut fingerprint_iter = fingerprints.iter();
36783687
for info in address_list {
3688+
contact_ids.push(None);
36793689
let addr = &info.addr;
36803690
if !may_be_valid_addr(addr) {
3681-
contact_ids.push(None);
36823691
continue;
36833692
}
36843693
let fingerprint: String = if let Some(fp) = fingerprint_iter.next() {
@@ -3690,24 +3699,24 @@ async fn add_or_lookup_key_contacts(
36903699
contact_ids.push(Some(ContactId::SELF));
36913700
continue;
36923701
} else {
3693-
contact_ids.push(None);
36943702
continue;
36953703
};
36963704
let display_name = info.display_name.as_deref();
3697-
if let Ok(addr) = ContactAddress::new(addr) {
3698-
let (contact_id, _) = Contact::add_or_lookup_ex(
3699-
context,
3700-
display_name.unwrap_or_default(),
3701-
&addr,
3702-
&fingerprint,
3703-
origin,
3704-
)
3705-
.await?;
3706-
contact_ids.push(Some(contact_id));
3707-
} else {
3705+
let Ok(addr) = ContactAddress::new(addr) else {
37083706
warn!(context, "Contact with address {:?} cannot exist.", addr);
3709-
contact_ids.push(None);
3710-
}
3707+
continue;
3708+
};
3709+
let res = Contact::add_or_lookup_ex(
3710+
context,
3711+
display_name.unwrap_or_default(),
3712+
&addr,
3713+
&fingerprint,
3714+
origin,
3715+
)
3716+
.await;
3717+
let contact_id = logged_debug_assert_ok!(context, res).ok().map(|(id, _)| id);
3718+
contact_ids.pop();
3719+
contact_ids.push(contact_id);
37113720
}
37123721

37133722
ensure_and_debug_assert_eq!(contact_ids.len(), address_list.len(),);
@@ -3856,35 +3865,36 @@ async fn lookup_key_contacts_by_address_list(
38563865
let mut contact_ids = Vec::new();
38573866
let mut fingerprint_iter = fingerprints.iter();
38583867
for info in address_list {
3868+
contact_ids.push(None);
38593869
let addr = &info.addr;
38603870
if !may_be_valid_addr(addr) {
3861-
contact_ids.push(None);
38623871
continue;
38633872
}
38643873

3865-
if let Some(fp) = fingerprint_iter.next() {
3874+
let contact_id = if let Some(fp) = fingerprint_iter.next() {
38663875
// Iterator has not ran out of fingerprints yet.
38673876
let display_name = info.display_name.as_deref();
38683877
let fingerprint: String = fp.hex();
38693878

3870-
if let Ok(addr) = ContactAddress::new(addr) {
3871-
let (contact_id, _) = Contact::add_or_lookup_ex(
3872-
context,
3873-
display_name.unwrap_or_default(),
3874-
&addr,
3875-
&fingerprint,
3876-
Origin::Hidden,
3877-
)
3878-
.await?;
3879-
contact_ids.push(Some(contact_id));
3880-
} else {
3879+
let Ok(addr) = ContactAddress::new(addr) else {
38813880
warn!(context, "Contact with address {:?} cannot exist.", addr);
3882-
contact_ids.push(None);
3883-
}
3881+
continue;
3882+
};
3883+
let res = Contact::add_or_lookup_ex(
3884+
context,
3885+
display_name.unwrap_or_default(),
3886+
&addr,
3887+
&fingerprint,
3888+
Origin::Hidden,
3889+
)
3890+
.await;
3891+
logged_debug_assert_ok!(context, res).ok().map(|(id, _)| id)
38843892
} else {
3885-
let contact_id = lookup_key_contact_by_address(context, addr, chat_id).await?;
3886-
contact_ids.push(contact_id);
3887-
}
3893+
let res = lookup_key_contact_by_address(context, addr, chat_id).await;
3894+
logged_debug_assert_ok!(context, res).ok().flatten()
3895+
};
3896+
contact_ids.pop();
3897+
contact_ids.push(contact_id);
38883898
}
38893899
ensure_and_debug_assert_eq!(address_list.len(), contact_ids.len(),);
38903900
Ok(contact_ids)

src/tools.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,5 +815,17 @@ macro_rules! logged_debug_assert {
815815
};
816816
}
817817

818+
/// Logs a warning if the second arg is an error. Evaluates to the second arg value.
819+
/// In non-optimized builds, panics also on error.
820+
#[macro_export]
821+
macro_rules! logged_debug_assert_ok {
822+
($ctx:expr, $res:expr) => {{
823+
let res_val = $res;
824+
let res_val = res_val.log_err($ctx);
825+
debug_assert!(res_val.is_ok());
826+
res_val
827+
}};
828+
}
829+
818830
#[cfg(test)]
819831
mod tools_tests;

0 commit comments

Comments
 (0)