notmuch: improved error handling and logging

This commit is contained in:
Bill Thiede 2024-12-15 14:44:02 -08:00
parent a0eb8dcba6
commit 05cdcec244

View File

@ -213,7 +213,7 @@ use std::{
process::Command, process::Command,
}; };
use log::info; use log::{error, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Number of seconds since the Epoch /// # Number of seconds since the Epoch
@ -507,14 +507,19 @@ impl Notmuch {
) -> Result<SearchSummary, NotmuchError> { ) -> Result<SearchSummary, NotmuchError> {
let query = if query.is_empty() { "*" } else { query }; let query = if query.is_empty() { "*" } else { query };
let res = self.run_notmuch([ let res = self
.run_notmuch([
"search", "search",
"--format=json", "--format=json",
&format!("--offset={offset}"), &format!("--offset={offset}"),
&format!("--limit={limit}"), &format!("--limit={limit}"),
query, query,
])?; ])
Ok(serde_json::from_slice(&res)?) .inspect_err(|err| error!("failed to notmuch search for query '{query}': {err}"))?;
Ok(serde_json::from_slice(&res).unwrap_or_else(|err| {
error!("failed to decode search result for query '{query}': {err}");
SearchSummary(Vec::new())
}))
} }
pub fn count(&self, query: &str) -> Result<usize, NotmuchError> { pub fn count(&self, query: &str) -> Result<usize, NotmuchError> {
@ -523,8 +528,10 @@ impl Notmuch {
// let res = self.run_notmuch(["count", "--output=threads", query])?; // let res = self.run_notmuch(["count", "--output=threads", query])?;
let res = self.run_notmuch(["count", query])?; let res = self.run_notmuch(["count", query])?;
// Strip '\n' from res. // Strip '\n' from res.
let s = std::str::from_utf8(&res[..res.len() - 1])?; let s = std::str::from_utf8(&res)?.trim();
Ok(s.parse()?) Ok(s.parse()
.inspect_err(|err| error!("failed to parse count for query '{query}': {err}"))
.unwrap_or(0))
} }
pub fn show(&self, query: &str) -> Result<ThreadSet, NotmuchError> { pub fn show(&self, query: &str) -> Result<ThreadSet, NotmuchError> {