refactor: resultify set_msg_failed()

This commit is contained in:
link2xt
2023-07-08 23:59:45 +00:00
parent 7aac4bfc83
commit 27e177dc05
4 changed files with 33 additions and 33 deletions

View File

@@ -2439,7 +2439,7 @@ async fn create_send_msg_job(context: &Context, msg_id: MsgId) -> Result<Option<
let rendered_msg = match mimefactory.render(context).await { let rendered_msg = match mimefactory.render(context).await {
Ok(res) => Ok(res), Ok(res) => Ok(res),
Err(err) => { Err(err) => {
message::set_msg_failed(context, msg_id, &err.to_string()).await; message::set_msg_failed(context, msg_id, &err.to_string()).await?;
Err(err) Err(err)
} }
}?; }?;
@@ -2451,7 +2451,7 @@ async fn create_send_msg_job(context: &Context, msg_id: MsgId) -> Result<Option<
msg_id, msg_id,
"End-to-end-encryption unavailable unexpectedly.", "End-to-end-encryption unavailable unexpectedly.",
) )
.await; .await?;
bail!( bail!(
"e2e encryption unavailable {} - {:?}", "e2e encryption unavailable {} - {:?}",
msg_id, msg_id,

View File

@@ -1649,8 +1649,9 @@ pub(crate) async fn update_msg_state(
// Context functions to work with messages // Context functions to work with messages
pub(crate) async fn set_msg_failed(context: &Context, msg_id: MsgId, error: &str) { pub(crate) async fn set_msg_failed(context: &Context, msg_id: MsgId, error: &str) -> Result<()> {
if let Ok(mut msg) = Message::load_from_db(context, msg_id).await { let mut msg = Message::load_from_db(context, msg_id).await?;
if msg.state.can_fail() { if msg.state.can_fail() {
msg.state = MessageState::OutFailed; msg.state = MessageState::OutFailed;
warn!(context, "{} failed: {}", msg_id, error); warn!(context, "{} failed: {}", msg_id, error);
@@ -1661,23 +1662,20 @@ pub(crate) async fn set_msg_failed(context: &Context, msg_id: MsgId, error: &str
) )
} }
match context context
.sql .sql
.execute( .execute(
"UPDATE msgs SET state=?, error=? WHERE id=?;", "UPDATE msgs SET state=?, error=? WHERE id=?;",
(msg.state, error, msg_id), (msg.state, error, msg_id),
) )
.await .await?;
{
Ok(_) => context.emit_event(EventType::MsgFailed { context.emit_event(EventType::MsgFailed {
chat_id: msg.chat_id, chat_id: msg.chat_id,
msg_id, msg_id,
}), });
Err(e) => {
warn!(context, "{:?}", e); Ok(())
}
}
}
} }
/// The number of messages assigned to unblocked chats /// The number of messages assigned to unblocked chats
@@ -2284,7 +2282,7 @@ mod tests {
update_msg_state(&alice, alice_msg.id, MessageState::OutMdnRcvd).await?; update_msg_state(&alice, alice_msg.id, MessageState::OutMdnRcvd).await?;
assert_state(&alice, alice_msg.id, MessageState::OutMdnRcvd).await; assert_state(&alice, alice_msg.id, MessageState::OutMdnRcvd).await;
set_msg_failed(&alice, alice_msg.id, "badly failed").await; set_msg_failed(&alice, alice_msg.id, "badly failed").await?;
assert_state(&alice, alice_msg.id, MessageState::OutFailed).await; assert_state(&alice, alice_msg.id, MessageState::OutFailed).await;
// check incoming message states on receiver side // check incoming message states on receiver side

View File

@@ -2156,7 +2156,7 @@ async fn handle_ndn(
let mut first = true; let mut first = true;
for msg in msgs { for msg in msgs {
let (msg_id, chat_id, chat_type) = msg?; let (msg_id, chat_id, chat_type) = msg?;
set_msg_failed(context, msg_id, &error).await; set_msg_failed(context, msg_id, &error).await?;
if first { if first {
// Add only one info msg for all failed messages // Add only one info msg for all failed messages
ndn_maybe_add_info_msg(context, failed, chat_id, chat_type).await?; ndn_maybe_add_info_msg(context, failed, chat_id, chat_type).await?;

View File

@@ -492,7 +492,9 @@ pub(crate) async fn smtp_send(
if let SendResult::Failure(err) = &status { if let SendResult::Failure(err) = &status {
// We couldn't send the message, so mark it as failed // We couldn't send the message, so mark it as failed
message::set_msg_failed(context, msg_id, &err.to_string()).await; if let Err(err) = message::set_msg_failed(context, msg_id, &err.to_string()).await {
error!(context, "Failed to mark {msg_id} as failed: {err:#}.");
}
} }
status status
} }
@@ -539,7 +541,7 @@ pub(crate) async fn send_msg_to_smtp(
) )
.await?; .await?;
if retries > 6 { if retries > 6 {
message::set_msg_failed(context, msg_id, "Number of retries exceeded the limit.").await; message::set_msg_failed(context, msg_id, "Number of retries exceeded the limit.").await?;
context context
.sql .sql
.execute("DELETE FROM smtp WHERE id=?", (rowid,)) .execute("DELETE FROM smtp WHERE id=?", (rowid,))