From 82ace72527648ef2dc32e08cf177880732394565 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Thu, 30 Mar 2023 10:13:07 +0200 Subject: [PATCH] ref(logging): Remove message from LogExt::log_err (#4235) This removes the message that needed to be supplied to LogExt::log_err calls. This was from a time before we adopted anyhow and now we are better off using anyhow::Context::context for the message: it is more consistent, composes better and is less custom. The benefit of the composition can be seen in the FFI calls which need to both log the error as well as return it to the caller via the set_last_error mechanism. It also removes the LogExt::ok_or_log_msg funcion for the same reason, the message is obsoleted by anyhow's context. --- deltachat-ffi/src/lib.rs | 181 ++++++++++++++++++++++++++------------- src/configure.rs | 4 +- src/imap/scan_folders.rs | 10 ++- src/log.rs | 41 ++------- src/receive_imf.rs | 6 +- src/sql.rs | 4 +- 6 files changed, 145 insertions(+), 101 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 968616a5d..7daa9d195 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -160,7 +160,8 @@ pub unsafe extern "C" fn dc_context_open( let ctx = &*context; let passphrase = to_string_lossy(passphrase); block_on(ctx.open(passphrase)) - .log_err(ctx, "dc_context_open() failed") + .context("dc_context_open() failed") + .log_err(ctx) .map(|b| b as libc::c_int) .unwrap_or(0) } @@ -216,16 +217,18 @@ pub unsafe extern "C" fn dc_set_config( if key.starts_with("ui.") { ctx.set_ui_config(&key, value.as_deref()) .await - .with_context(|| format!("Can't set {key} to {value:?}")) - .log_err(ctx, "dc_set_config() failed") + .with_context(|| format!("dc_set_config failed: Can't set {key} to {value:?}")) + .log_err(ctx) .is_ok() as libc::c_int } else { match config::Config::from_str(&key) { Ok(key) => ctx .set_config(key, value.as_deref()) .await - .with_context(|| format!("Can't set {key} to {value:?}")) - .log_err(ctx, "dc_set_config() failed") + .with_context(|| { + format!("dc_set_config() failed: Can't set {key} to {value:?}") + }) + .log_err(ctx) .is_ok() as libc::c_int, Err(_) => { warn!(ctx, "dc_set_config(): invalid key"); @@ -253,7 +256,8 @@ pub unsafe extern "C" fn dc_get_config( if key.starts_with("ui.") { ctx.get_ui_config(&key) .await - .log_err(ctx, "Can't get ui-config") + .context("Can't get ui-config") + .log_err(ctx) .unwrap_or_default() .unwrap_or_default() .strdup() @@ -262,7 +266,8 @@ pub unsafe extern "C" fn dc_get_config( Ok(key) => ctx .get_config(key) .await - .log_err(ctx, "Can't get config") + .context("Can't get config") + .log_err(ctx) .unwrap_or_default() .unwrap_or_default() .strdup(), @@ -414,7 +419,8 @@ pub unsafe extern "C" fn dc_get_oauth2_url( block_on(async move { match oauth2::get_oauth2_url(ctx, &addr, &redirect) .await - .log_err(ctx, "dc_get_oauth2_url failed") + .context("dc_get_oauth2_url failed") + .log_err(ctx) { Ok(Some(res)) => res.strdup(), Ok(None) | Err(_) => ptr::null_mut(), @@ -423,7 +429,12 @@ pub unsafe extern "C" fn dc_get_oauth2_url( } fn spawn_configure(ctx: Context) { - spawn(async move { ctx.configure().await.log_err(&ctx, "Configure failed") }); + spawn(async move { + ctx.configure() + .await + .context("Configure failed") + .log_err(&ctx) + }); } #[no_mangle] @@ -448,7 +459,8 @@ pub unsafe extern "C" fn dc_is_configured(context: *mut dc_context_t) -> libc::c block_on(async move { ctx.is_configured() .await - .log_err(ctx, "failed to get configured state") + .context("failed to get configured state") + .log_err(ctx) .unwrap_or_default() as libc::c_int }) } @@ -790,7 +802,8 @@ pub unsafe extern "C" fn dc_preconfigure_keypair( key::store_self_keypair(ctx, &keypair, key::KeyPairUse::Default).await?; Ok::<_, anyhow::Error>(1) }) - .log_err(ctx, "Failed to save keypair") + .context("Failed to save keypair") + .log_err(ctx) .unwrap_or(0) } @@ -817,7 +830,8 @@ pub unsafe extern "C" fn dc_get_chatlist( block_on(async move { match chatlist::Chatlist::try_load(ctx, flags as usize, qs.as_deref(), qi) .await - .log_err(ctx, "Failed to get chatlist") + .context("Failed to get chatlist") + .log_err(ctx) { Ok(list) => { let ffi_list = ChatlistWrapper { context, list }; @@ -842,7 +856,8 @@ pub unsafe extern "C" fn dc_create_chat_by_contact_id( block_on(async move { ChatId::create_for_contact(ctx, ContactId::new(contact_id)) .await - .log_err(ctx, "Failed to create chat from contact_id") + .context("Failed to create chat from contact_id") + .log_err(ctx) .map(|id| id.to_u32()) .unwrap_or(0) }) @@ -862,7 +877,8 @@ pub unsafe extern "C" fn dc_get_chat_id_by_contact_id( block_on(async move { ChatId::lookup_by_contact(ctx, ContactId::new(contact_id)) .await - .log_err(ctx, "Failed to get chat for contact_id") + .context("Failed to get chat for contact_id") + .log_err(ctx) .unwrap_or_default() // unwraps the Result .map(|id| id.to_u32()) .unwrap_or(0) // unwraps the Option @@ -1004,7 +1020,8 @@ pub unsafe extern "C" fn dc_get_msg_reactions( let ctx = &*context; let reactions = if let Ok(reactions) = block_on(get_msg_reactions(ctx, MsgId::new(msg_id))) - .log_err(ctx, "failed dc_get_msg_reactions() call") + .context("failed dc_get_msg_reactions() call") + .log_err(ctx) { reactions } else { @@ -1032,7 +1049,8 @@ pub unsafe extern "C" fn dc_send_webxdc_status_update( &to_string_lossy(json), &to_string_lossy(descr), )) - .log_err(ctx, "Failed to send webxdc update") + .context("Failed to send webxdc update") + .log_err(ctx) .is_ok() as libc::c_int } @@ -1251,7 +1269,8 @@ pub unsafe extern "C" fn dc_get_fresh_msgs( let arr = dc_array_t::from( ctx.get_fresh_msgs() .await - .log_err(ctx, "Failed to get fresh messages") + .context("Failed to get fresh messages") + .log_err(ctx) .unwrap_or_default() .iter() .map(|msg_id| msg_id.to_u32()) @@ -1272,7 +1291,8 @@ pub unsafe extern "C" fn dc_marknoticed_chat(context: *mut dc_context_t, chat_id block_on(async move { chat::marknoticed_chat(ctx, ChatId::new(chat_id)) .await - .log_err(ctx, "Failed marknoticed chat") + .context("Failed marknoticed chat") + .log_err(ctx) .unwrap_or(()) }) } @@ -1414,7 +1434,8 @@ pub unsafe extern "C" fn dc_set_chat_visibility( ChatId::new(chat_id) .set_visibility(ctx, visibility) .await - .log_err(ctx, "Failed setting chat visibility") + .context("Failed setting chat visibility") + .log_err(ctx) .unwrap_or(()) }) } @@ -1431,7 +1452,9 @@ pub unsafe extern "C" fn dc_delete_chat(context: *mut dc_context_t, chat_id: u32 ChatId::new(chat_id) .delete(ctx) .await - .ok_or_log_msg(ctx, "Failed chat delete"); + .context("Failed chat delete") + .log_err(ctx) + .ok(); }) } @@ -1447,7 +1470,9 @@ pub unsafe extern "C" fn dc_block_chat(context: *mut dc_context_t, chat_id: u32) ChatId::new(chat_id) .block(ctx) .await - .ok_or_log_msg(ctx, "Failed chat block"); + .context("Failed chat block") + .log_err(ctx) + .ok(); }) } @@ -1463,7 +1488,9 @@ pub unsafe extern "C" fn dc_accept_chat(context: *mut dc_context_t, chat_id: u32 ChatId::new(chat_id) .accept(ctx) .await - .ok_or_log_msg(ctx, "Failed chat accept"); + .context("Failed chat accept") + .log_err(ctx) + .ok(); }) } @@ -1561,7 +1588,8 @@ pub unsafe extern "C" fn dc_create_group_chat( block_on(async move { chat::create_group_chat(ctx, protect, &to_string_lossy(name)) .await - .log_err(ctx, "Failed to create group chat") + .context("Failed to create group chat") + .log_err(ctx) .map(|id| id.to_u32()) .unwrap_or(0) }) @@ -1575,7 +1603,8 @@ pub unsafe extern "C" fn dc_create_broadcast_list(context: *mut dc_context_t) -> } let ctx = &*context; block_on(chat::create_broadcast_list(ctx)) - .log_err(ctx, "Failed to create broadcast list") + .context("Failed to create broadcast list") + .log_err(ctx) .map(|id| id.to_u32()) .unwrap_or(0) } @@ -1597,7 +1626,8 @@ pub unsafe extern "C" fn dc_is_contact_in_chat( ChatId::new(chat_id), ContactId::new(contact_id), )) - .log_err(ctx, "is_contact_in_chat failed") + .context("is_contact_in_chat failed") + .log_err(ctx) .unwrap_or_default() as libc::c_int } @@ -1618,7 +1648,8 @@ pub unsafe extern "C" fn dc_add_contact_to_chat( ChatId::new(chat_id), ContactId::new(contact_id), )) - .log_err(ctx, "Failed to add contact") + .context("Failed to add contact") + .log_err(ctx) .is_ok() as libc::c_int } @@ -1639,7 +1670,8 @@ pub unsafe extern "C" fn dc_remove_contact_from_chat( ChatId::new(chat_id), ContactId::new(contact_id), )) - .log_err(ctx, "Failed to remove contact") + .context("Failed to remove contact") + .log_err(ctx) .is_ok() as libc::c_int } @@ -1758,7 +1790,8 @@ pub unsafe extern "C" fn dc_get_chat_ephemeral_timer( // ignored when ephemeral timer value is used to construct // message headers. block_on(async move { ChatId::new(chat_id).get_ephemeral_timer(ctx).await }) - .log_err(ctx, "Failed to get ephemeral timer") + .context("Failed to get ephemeral timer") + .log_err(ctx) .unwrap_or_default() .to_u32() } @@ -1779,7 +1812,8 @@ pub unsafe extern "C" fn dc_set_chat_ephemeral_timer( ChatId::new(chat_id) .set_ephemeral_timer(ctx, EphemeralTimer::from_u32(timer)) .await - .log_err(ctx, "Failed to set ephemeral timer") + .context("Failed to set ephemeral timer") + .log_err(ctx) .is_ok() as libc::c_int }) } @@ -1855,7 +1889,8 @@ pub unsafe extern "C" fn dc_delete_msgs( let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); block_on(message::delete_msgs(ctx, &msg_ids)) - .log_err(ctx, "failed dc_delete_msgs() call") + .context("failed dc_delete_msgs() call") + .log_err(ctx) .ok(); } @@ -1919,7 +1954,8 @@ pub unsafe extern "C" fn dc_markseen_msgs( let ctx = &*context; block_on(message::markseen_msgs(ctx, msg_ids)) - .log_err(ctx, "failed dc_markseen_msgs() call") + .context("failed dc_markseen_msgs() call") + .log_err(ctx) .ok(); } @@ -1961,7 +1997,8 @@ pub unsafe extern "C" fn dc_download_full_msg(context: *mut dc_context_t, msg_id } let ctx = &*context; block_on(MsgId::new(msg_id).download_full(ctx)) - .log_err(ctx, "Failed to download message fully.") + .context("Failed to download message fully.") + .log_err(ctx) .ok(); } @@ -2009,7 +2046,8 @@ pub unsafe extern "C" fn dc_create_contact( let name = to_string_lossy(name); block_on(Contact::create(ctx, &name, &to_string_lossy(addr))) - .log_err(ctx, "Cannot create contact") + .context("Cannot create contact") + .log_err(ctx) .map(|id| id.to_u32()) .unwrap_or(0) } @@ -2086,7 +2124,8 @@ pub unsafe extern "C" fn dc_get_blocked_contacts( Box::into_raw(Box::new(dc_array_t::from( Contact::get_all_blocked(ctx) .await - .log_err(ctx, "Can't get blocked contacts") + .context("Can't get blocked contacts") + .log_err(ctx) .unwrap_or_default() .iter() .map(|id| id.to_u32()) @@ -2111,11 +2150,15 @@ pub unsafe extern "C" fn dc_block_contact( if block == 0 { Contact::unblock(ctx, contact_id) .await - .ok_or_log_msg(ctx, "Can't unblock contact"); + .context("Can't unblock contact") + .log_err(ctx) + .ok(); } else { Contact::block(ctx, contact_id) .await - .ok_or_log_msg(ctx, "Can't block contact"); + .context("Can't block contact") + .log_err(ctx) + .ok(); } }); } @@ -2188,7 +2231,8 @@ fn spawn_imex(ctx: Context, what: imex::ImexMode, param1: String, passphrase: Op spawn(async move { imex::imex(&ctx, what, param1.as_ref(), passphrase) .await - .log_err(&ctx, "IMEX failed") + .context("IMEX failed") + .log_err(&ctx) }); } @@ -2374,7 +2418,8 @@ pub unsafe extern "C" fn dc_join_securejoin( securejoin::join_securejoin(ctx, &to_string_lossy(qr)) .await .map(|chatid| chatid.to_u32()) - .log_err(ctx, "failed dc_join_securejoin() call") + .context("failed dc_join_securejoin() call") + .log_err(ctx) .unwrap_or_default() }) } @@ -2396,7 +2441,8 @@ pub unsafe extern "C" fn dc_send_locations_to_chat( ChatId::new(chat_id), seconds as i64, )) - .log_err(ctx, "Failed dc_send_locations_to_chat()") + .context("Failed dc_send_locations_to_chat()") + .log_err(ctx) .ok(); } @@ -2479,7 +2525,8 @@ pub unsafe extern "C" fn dc_delete_all_locations(context: *mut dc_context_t) { block_on(async move { location::delete_all(ctx) .await - .log_err(ctx, "Failed to delete locations") + .context("Failed to delete locations") + .log_err(ctx) .ok() }); } @@ -2763,7 +2810,8 @@ pub unsafe extern "C" fn dc_chatlist_get_summary( .list .get_summary(ctx, index, maybe_chat) .await - .log_err(ctx, "get_summary failed") + .context("get_summary failed") + .log_err(ctx) .unwrap_or_default(); Box::into_raw(Box::new(summary.into())) }) @@ -2791,7 +2839,8 @@ pub unsafe extern "C" fn dc_chatlist_get_summary2( msg_id, None, )) - .log_err(ctx, "get_summary2 failed") + .context("get_summary2 failed") + .log_err(ctx) .unwrap_or_default(); Box::into_raw(Box::new(summary.into())) } @@ -2974,7 +3023,8 @@ pub unsafe extern "C" fn dc_chat_can_send(chat: *mut dc_chat_t) -> libc::c_int { let ffi_chat = &*chat; let ctx = &*ffi_chat.context; block_on(ffi_chat.chat.can_send(ctx)) - .log_err(ctx, "can_send failed") + .context("can_send failed") + .log_err(ctx) .unwrap_or_default() as libc::c_int } @@ -3419,7 +3469,8 @@ pub unsafe extern "C" fn dc_msg_get_summary( let ctx = &*ffi_msg.context; let summary = block_on(ffi_msg.message.get_summary(ctx, maybe_chat)) - .log_err(ctx, "dc_msg_get_summary failed") + .context("dc_msg_get_summary failed") + .log_err(ctx) .unwrap_or_default(); Box::into_raw(Box::new(summary.into())) } @@ -3437,7 +3488,8 @@ pub unsafe extern "C" fn dc_msg_get_summarytext( let ctx = &*ffi_msg.context; let summary = block_on(ffi_msg.message.get_summary(ctx, None)) - .log_err(ctx, "dc_msg_get_summarytext failed") + .context("dc_msg_get_summarytext failed") + .log_err(ctx) .unwrap_or_default(); match usize::try_from(approx_characters) { Ok(chars) => summary.truncated_text(chars).strdup(), @@ -3704,7 +3756,9 @@ pub unsafe extern "C" fn dc_msg_latefiling_mediasize( .message .latefiling_mediasize(ctx, width, height, duration) }) - .ok_or_log_msg(ctx, "Cannot set media size"); + .context("Cannot set media size") + .log_err(ctx) + .ok(); } #[no_mangle] @@ -3743,7 +3797,8 @@ pub unsafe extern "C" fn dc_msg_set_quote(msg: *mut dc_msg_t, quote: *const dc_m .message .set_quote(&*ffi_msg.context, quote_msg) .await - .log_err(&*ffi_msg.context, "failed to set quote") + .context("failed to set quote") + .log_err(&*ffi_msg.context) .ok(); }); } @@ -3774,7 +3829,8 @@ pub unsafe extern "C" fn dc_msg_get_quoted_msg(msg: *const dc_msg_t) -> *mut dc_ .message .quoted_message(context) .await - .log_err(context, "failed to get quoted message") + .context("failed to get quoted message") + .log_err(context) .unwrap_or(None) }); @@ -3797,7 +3853,8 @@ pub unsafe extern "C" fn dc_msg_get_parent(msg: *const dc_msg_t) -> *mut dc_msg_ .message .parent(context) .await - .log_err(context, "failed to get parent message") + .context("failed to get parent message") + .log_err(context) .unwrap_or(None) }); @@ -3988,7 +4045,8 @@ pub unsafe extern "C" fn dc_contact_is_verified(contact: *mut dc_contact_t) -> l let ctx = &*ffi_contact.context; block_on(ffi_contact.contact.is_verified(ctx)) - .log_err(ctx, "is_verified failed") + .context("is_verified failed") + .log_err(ctx) .unwrap_or_default() as libc::c_int } @@ -4003,7 +4061,8 @@ pub unsafe extern "C" fn dc_contact_get_verifier_addr( let ffi_contact = &*contact; let ctx = &*ffi_contact.context; block_on(ffi_contact.contact.get_verifier_addr(ctx)) - .log_err(ctx, "failed to get verifier for contact") + .context("failed to get verifier for contact") + .log_err(ctx) .unwrap_or_default() .strdup() } @@ -4017,7 +4076,8 @@ pub unsafe extern "C" fn dc_contact_get_verifier_id(contact: *mut dc_contact_t) let ffi_contact = &*contact; let ctx = &*ffi_contact.context; let verifier_contact_id = block_on(ffi_contact.contact.get_verifier_id(ctx)) - .log_err(ctx, "failed to get verifier") + .context("failed to get verifier") + .log_err(ctx) .unwrap_or_default() .unwrap_or_default(); @@ -4169,8 +4229,8 @@ pub unsafe extern "C" fn dc_backup_provider_new( provider, }) .map(|ffi_provider| Box::into_raw(Box::new(ffi_provider))) - .log_err(ctx, "BackupProvider failed") .context("BackupProvider failed") + .log_err(ctx) .set_last_error(ctx) .unwrap_or(ptr::null_mut()) } @@ -4186,8 +4246,8 @@ pub unsafe extern "C" fn dc_backup_provider_get_qr( let ffi_provider = &*provider; let ctx = &*ffi_provider.context; deltachat::qr::format_backup(&ffi_provider.provider.qr()) - .log_err(ctx, "BackupProvider get_qr failed") .context("BackupProvider get_qr failed") + .log_err(ctx) .set_last_error(ctx) .unwrap_or_default() .strdup() @@ -4205,8 +4265,8 @@ pub unsafe extern "C" fn dc_backup_provider_get_qr_svg( let ctx = &*ffi_provider.context; let provider = &ffi_provider.provider; block_on(generate_backup_qr(ctx, &provider.qr())) - .log_err(ctx, "BackupProvider get_qr_svg failed") .context("BackupProvider get_qr_svg failed") + .log_err(ctx) .set_last_error(ctx) .unwrap_or_default() .strdup() @@ -4222,8 +4282,8 @@ pub unsafe extern "C" fn dc_backup_provider_wait(provider: *mut dc_backup_provid let ctx = &*ffi_provider.context; let provider = &mut ffi_provider.provider; block_on(provider) - .log_err(ctx, "Failed to await BackupProvider") .context("Failed to await BackupProvider") + .log_err(ctx) .set_last_error(ctx) .ok(); } @@ -4255,16 +4315,16 @@ pub unsafe extern "C" fn dc_receive_backup( // user from deallocating it by calling unref on the object while we are using it. fn receive_backup(ctx: Context, qr_text: String) -> libc::c_int { let qr = match block_on(qr::check_qr(&ctx, &qr_text)) - .log_err(&ctx, "Invalid QR code") .context("Invalid QR code") + .log_err(&ctx) .set_last_error(&ctx) { Ok(qr) => qr, Err(_) => return 0, }; match block_on(imex::get_backup(&ctx, qr)) - .log_err(&ctx, "Get backup failed") .context("Get backup failed") + .log_err(&ctx) .set_last_error(&ctx) { Ok(_) => 1, @@ -4316,8 +4376,8 @@ where /// /// ```no_compile /// some_dc_rust_api_call_returning_result() - /// .log_err(&context, "My API call failed") /// .context("My API call failed") + /// .log_err(&context) /// .set_last_error(&context) /// .unwrap_or_default() /// ``` @@ -4404,7 +4464,8 @@ pub unsafe extern "C" fn dc_provider_new_from_email_with_dns( let socks5_enabled = block_on(async move { ctx.get_config_bool(config::Config::Socks5Enabled) .await - .log_err(ctx, "Can't get config") + .context("Can't get config") + .log_err(ctx) }); match socks5_enabled { diff --git a/src/configure.rs b/src/configure.rs index eb69ad25c..71e19dd56 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -156,7 +156,9 @@ async fn on_configure_completed( Some(stock_str::aeap_explanation_and_link(context, &old_addr, &new_addr).await); chat::add_device_msg(context, None, Some(&mut msg)) .await - .ok_or_log_msg(context, "Cannot add AEAP explanation"); + .context("Cannot add AEAP explanation") + .log_err(context) + .ok(); } } } diff --git a/src/imap/scan_folders.rs b/src/imap/scan_folders.rs index 29051e7b9..8b63098b8 100644 --- a/src/imap/scan_folders.rs +++ b/src/imap/scan_folders.rs @@ -70,7 +70,9 @@ impl Imap { loop { self.fetch_move_delete(context, folder.name(), folder_meaning) .await - .ok_or_log_msg(context, "Can't fetch new msgs in scanned folder"); + .context("Can't fetch new msgs in scanned folder") + .log_err(context) + .ok(); let session = self.session.as_mut().context("no session")?; // If the server sent an unsocicited EXISTS during the fetch, we need to fetch again @@ -105,7 +107,11 @@ impl Imap { let list = session .list(Some(""), Some("*")) .await? - .filter_map(|f| async { f.ok_or_log_msg(context, "list_folders() can't get folder") }); + .filter_map(|f| async { + f.context("list_folders() can't get folder") + .log_err(context) + .ok() + }); Ok(list.collect().await) } } diff --git a/src/log.rs b/src/log.rs index c755484e6..10daed67c 100644 --- a/src/log.rs +++ b/src/log.rs @@ -66,7 +66,7 @@ where Self: std::marker::Sized, { #[track_caller] - fn log_err_inner(self, context: &Context, msg: Option<&str>) -> Result; + fn log_err_inner(self, context: &Context) -> Result; /// Emits a warning if the receiver contains an Err value. /// @@ -79,8 +79,8 @@ where /// like warn!(), since the file!() and line!() macros don't work with track_caller) /// See for progress on this. #[track_caller] - fn log_err(self, context: &Context, msg: &str) -> Result { - self.log_err_inner(context, Some(msg)) + fn log_err(self, context: &Context) -> Result { + self.log_err_inner(context) } /// Emits a warning if the receiver contains an Err value and returns an [`Option`]. @@ -99,50 +99,21 @@ where /// For a note on the `track_caller` feature, see the doc comment on `log_err()`. #[track_caller] fn ok_or_log(self, context: &Context) -> Option { - self.log_err_inner(context, None).ok() - } - - /// Like `ok_or_log()`, but you can pass an extra message that is prepended in the log. - /// - /// Example: - /// ```text - /// if let Err(e) = do_something() { - /// warn!(context, "Something went wrong: {:#}", e); - /// } - /// ``` - /// is equivalent to: - /// ```text - /// do_something().ok_or_log_msg(context, "Something went wrong"); - /// ``` - /// and is also equivalent to: - /// ```text - /// use anyhow::Context as _; - /// do_something().context("Something went wrong").ok_or_log(context); - /// ``` - /// - /// For a note on the `track_caller` feature, see the doc comment on `log_err()`. - #[track_caller] - fn ok_or_log_msg(self, context: &Context, msg: &'static str) -> Option { - self.log_err_inner(context, Some(msg)).ok() + self.log_err_inner(context).ok() } } impl LogExt for Result { #[track_caller] - fn log_err_inner(self, context: &Context, msg: Option<&str>) -> Result { + fn log_err_inner(self, context: &Context) -> Result { if let Err(e) = &self { let location = std::panic::Location::caller(); - let separator = if msg.is_none() { "" } else { ": " }; - let msg = msg.unwrap_or_default(); - // We are using Anyhow's .context() and to show the inner error, too, we need the {:#}: let full = format!( - "{file}:{line}: {msg}{separator}{e:#}", + "{file}:{line}: {e:#}", file = location.file(), line = location.line(), - msg = msg, - separator = separator, e = e ); // We can't use the warn!() macro here as the file!() and line!() macros diff --git a/src/receive_imf.rs b/src/receive_imf.rs index c4adf7132..318c49bc0 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -691,7 +691,8 @@ async fn add_parts( } else if allow_creation { if let Ok(chat) = ChatIdBlocked::get_for_contact(context, from_id, create_blocked) .await - .log_err(context, "Failed to get (new) chat for contact") + .context("Failed to get (new) chat for contact") + .log_err(context) { chat_id = Some(chat.id); chat_id_blocked = chat.blocked; @@ -843,7 +844,8 @@ async fn add_parts( // maybe an Autocrypt Setup Message if let Ok(chat) = ChatIdBlocked::get_for_contact(context, ContactId::SELF, Blocked::Not) .await - .log_err(context, "Failed to get (new) chat for contact") + .context("Failed to get (new) chat for contact") + .log_err(context) { chat_id = Some(chat.id); chat_id_blocked = chat.blocked; diff --git a/src/sql.rs b/src/sql.rs index ff149f98c..26f07c756 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -757,7 +757,9 @@ pub async fn housekeeping(context: &Context) -> Result<()> { (), ) .await - .ok_or_log_msg(context, "failed to remove old MDNs"); + .context("failed to remove old MDNs") + .log_err(context) + .ok(); info!(context, "Housekeeping done."); Ok(())