From c385f96b835888dca117d4fef089cfa43aa9efd0 Mon Sep 17 00:00:00 2001 From: Shing Him Ng Date: Fri, 3 Jan 2025 09:03:31 -0600 Subject: [PATCH] Enumerate errors for peek_with_timeout --- payjoin-directory/src/db.rs | 9 +++++---- payjoin-directory/src/lib.rs | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/payjoin-directory/src/db.rs b/payjoin-directory/src/db.rs index 6165abf9..71c6408a 100644 --- a/payjoin-directory/src/db.rs +++ b/payjoin-directory/src/db.rs @@ -2,6 +2,7 @@ use std::time::Duration; use futures::StreamExt; use redis::{AsyncCommands, Client, ErrorKind, RedisError, RedisResult}; +use tokio::time::error::Elapsed; use tracing::debug; const DEFAULT_COLUMN: &str = ""; @@ -23,7 +24,7 @@ impl DbPool { self.push(subdirectory_id, DEFAULT_COLUMN, data).await } - pub async fn peek_default(&self, subdirectory_id: &str) -> Option>> { + pub async fn peek_default(&self, subdirectory_id: &str) -> Result>, Elapsed> { self.peek_with_timeout(subdirectory_id, DEFAULT_COLUMN).await } @@ -31,7 +32,7 @@ impl DbPool { self.push(subdirectory_id, PJ_V1_COLUMN, data).await } - pub async fn peek_v1(&self, subdirectory_id: &str) -> Option>> { + pub async fn peek_v1(&self, subdirectory_id: &str) -> Result>, Elapsed> { self.peek_with_timeout(subdirectory_id, PJ_V1_COLUMN).await } @@ -52,8 +53,8 @@ impl DbPool { &self, subdirectory_id: &str, channel_type: &str, - ) -> Option>> { - tokio::time::timeout(self.timeout, self.peek(subdirectory_id, channel_type)).await.ok() + ) -> Result>, Elapsed> { + tokio::time::timeout(self.timeout, self.peek(subdirectory_id, channel_type)).await } async fn peek(&self, subdirectory_id: &str, channel_type: &str) -> RedisResult> { diff --git a/payjoin-directory/src/lib.rs b/payjoin-directory/src/lib.rs index 9a1c651c..733b052e 100644 --- a/payjoin-directory/src/lib.rs +++ b/payjoin-directory/src/lib.rs @@ -341,11 +341,11 @@ async fn post_fallback_v1( .await .map_err(|e| HandlerError::BadRequest(e.into()))?; match pool.peek_v1(id).await { - Some(result) => match result { + Ok(result) => match result { Ok(buffered_req) => Ok(Response::new(full(buffered_req))), Err(e) => Err(HandlerError::BadRequest(e.into())), }, - None => Ok(none_response), + _ => Ok(none_response), } } @@ -409,11 +409,11 @@ async fn get_subdir( trace!("get_subdir"); let id = check_id_length(id)?; match pool.peek_default(id).await { - Some(result) => match result { + Ok(result) => match result { Ok(buffered_req) => Ok(Response::new(full(buffered_req))), Err(e) => Err(HandlerError::BadRequest(e.into())), }, - None => Ok(Response::builder().status(StatusCode::ACCEPTED).body(empty())?), + _ => Ok(Response::builder().status(StatusCode::ACCEPTED).body(empty())?), } }