mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 01:16:31 +03:00
refactor: resultify set_msg_failed()
This commit is contained in:
@@ -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,
|
||||||
|
|||||||
@@ -1649,35 +1649,33 @@ 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() {
|
|
||||||
msg.state = MessageState::OutFailed;
|
|
||||||
warn!(context, "{} failed: {}", msg_id, error);
|
|
||||||
} else {
|
|
||||||
warn!(
|
|
||||||
context,
|
|
||||||
"{} seems to have failed ({}), but state is {}", msg_id, error, msg.state
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
match context
|
if msg.state.can_fail() {
|
||||||
.sql
|
msg.state = MessageState::OutFailed;
|
||||||
.execute(
|
warn!(context, "{} failed: {}", msg_id, error);
|
||||||
"UPDATE msgs SET state=?, error=? WHERE id=?;",
|
} else {
|
||||||
(msg.state, error, msg_id),
|
warn!(
|
||||||
)
|
context,
|
||||||
.await
|
"{} seems to have failed ({}), but state is {}", msg_id, error, msg.state
|
||||||
{
|
)
|
||||||
Ok(_) => context.emit_event(EventType::MsgFailed {
|
|
||||||
chat_id: msg.chat_id,
|
|
||||||
msg_id,
|
|
||||||
}),
|
|
||||||
Err(e) => {
|
|
||||||
warn!(context, "{:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context
|
||||||
|
.sql
|
||||||
|
.execute(
|
||||||
|
"UPDATE msgs SET state=?, error=? WHERE id=?;",
|
||||||
|
(msg.state, error, msg_id),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
context.emit_event(EventType::MsgFailed {
|
||||||
|
chat_id: msg.chat_id,
|
||||||
|
msg_id,
|
||||||
|
});
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -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?;
|
||||||
|
|||||||
@@ -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,))
|
||||||
|
|||||||
Reference in New Issue
Block a user