diff --git a/notmuch/src/lib.rs b/notmuch/src/lib.rs index 0ffde83..7cf18b5 100644 --- a/notmuch/src/lib.rs +++ b/notmuch/src/lib.rs @@ -213,7 +213,7 @@ use std::{ process::Command, }; -use log::info; +use log::{error, info}; use serde::{Deserialize, Serialize}; /// # Number of seconds since the Epoch @@ -507,14 +507,19 @@ impl Notmuch { ) -> Result { let query = if query.is_empty() { "*" } else { query }; - let res = self.run_notmuch([ - "search", - "--format=json", - &format!("--offset={offset}"), - &format!("--limit={limit}"), - query, - ])?; - Ok(serde_json::from_slice(&res)?) + let res = self + .run_notmuch([ + "search", + "--format=json", + &format!("--offset={offset}"), + &format!("--limit={limit}"), + query, + ]) + .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 { @@ -523,8 +528,10 @@ impl Notmuch { // let res = self.run_notmuch(["count", "--output=threads", query])?; let res = self.run_notmuch(["count", query])?; // Strip '\n' from res. - let s = std::str::from_utf8(&res[..res.len() - 1])?; - Ok(s.parse()?) + let s = std::str::from_utf8(&res)?.trim(); + 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 {