refactor(db): add DbPool::with_conn_or for the default-and-log tails

Ten call sites across link_cache, state and queue repeated the same
match/if-let over a with_conn result with their own log line and default.
with_conn_or takes the level, the operation name and the default; each site
keeps its exact message and the same Ok/Err behaviour.
This commit is contained in:
2026-09-21 17:24:51 +08:00
parent f21570f579
commit d9f3ee99c1
4 changed files with 176 additions and 167 deletions
+20
View File
@@ -82,6 +82,26 @@ impl DbPool {
pub fn path(&self) -> &str {
&self.inner.path
}
/// [`with_conn`] for the many callers that answer a failed statement with
/// a default plus one log line: `what` names the operation and `level`
/// says how bad it is (`Error` when the failure loses work the caller
/// expected, `Warn` when the user is still served).
///
/// [`with_conn`]: DbPool::with_conn
pub async fn with_conn_or<T, F>(&self, level: log::Level, what: &str, default: T, f: F) -> T
where
T: Send + 'static,
F: FnOnce(&mut Connection) -> rusqlite::Result<T> + Send + 'static,
{
match self.with_conn(f).await {
Ok(value) => value,
Err(e) => {
log::log!(level, "{what}: {e}");
default
}
}
}
}
impl PoolInner {