From 9a88a32d5092cdacacc41bc8ec12ff56d4f53adf Mon Sep 17 00:00:00 2001 From: Prajjwal Kumar Date: Mon, 3 Jul 2023 18:21:23 +0530 Subject: [PATCH] feat: add `GenericNotFoundError` error response and `set_key_if_not_exists_with_expiry` Redis command (#1526) --- crates/drainer/src/utils.rs | 2 +- crates/redis_interface/src/commands.rs | 9 ++++++--- crates/router/src/compatibility/stripe/errors.rs | 8 +++++++- crates/router/src/core/errors/api_error_response.rs | 5 +++++ crates/router/src/db/queue.rs | 4 +++- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/crates/drainer/src/utils.rs b/crates/drainer/src/utils.rs index 7ba1018595..960ee5eca5 100644 --- a/crates/drainer/src/utils.rs +++ b/crates/drainer/src/utils.rs @@ -16,7 +16,7 @@ pub async fn is_stream_available(stream_index: u8, store: Arc) match store .redis_conn - .set_key_if_not_exist(stream_key_flag.as_str(), true) + .set_key_if_not_exists_with_expiry(stream_key_flag.as_str(), true, None) .await { Ok(resp) => resp == redis::types::SetnxReply::KeySet, diff --git a/crates/redis_interface/src/commands.rs b/crates/redis_interface/src/commands.rs index f88870a982..95fbe229e5 100644 --- a/crates/redis_interface/src/commands.rs +++ b/crates/redis_interface/src/commands.rs @@ -175,10 +175,11 @@ impl super::RedisConnectionPool { } #[instrument(level = "DEBUG", skip(self))] - pub async fn set_key_if_not_exist( + pub async fn set_key_if_not_exists_with_expiry( &self, key: &str, value: V, + seconds: Option, ) -> CustomResult where V: TryInto + Debug + Send + Sync, @@ -188,9 +189,11 @@ impl super::RedisConnectionPool { .set( key, value, - Some(Expiration::EX(self.config.default_ttl.into())), + Some(Expiration::EX( + seconds.unwrap_or(self.config.default_ttl.into()), + )), Some(SetOptions::NX), - false, + true, ) .await .into_report() diff --git a/crates/router/src/compatibility/stripe/errors.rs b/crates/router/src/compatibility/stripe/errors.rs index bd203170f3..7e341f071d 100644 --- a/crates/router/src/compatibility/stripe/errors.rs +++ b/crates/router/src/compatibility/stripe/errors.rs @@ -82,6 +82,9 @@ pub enum StripeErrorCode { #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such payment method")] PaymentMethodNotFound, + #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "{message}")] + GenericNotFoundError { message: String }, + #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such merchant account")] MerchantAccountNotFound, @@ -391,6 +394,9 @@ impl From for StripeErrorCode { param: field_names.clone().join(", "), } } + errors::ApiErrorResponse::GenericNotFoundError { message } => { + Self::GenericNotFoundError { message } + } // parameter unknown, invalid request error // actually if we type wrong values in address we get this error. Stripe throws parameter unknown. I don't know if stripe is validating email and stuff errors::ApiErrorResponse::InvalidDataFormat { field_name, @@ -535,7 +541,7 @@ impl actix_web::ResponseError for StripeErrorCode { match self { Self::Unauthorized => StatusCode::UNAUTHORIZED, - Self::InvalidRequestUrl => StatusCode::NOT_FOUND, + Self::InvalidRequestUrl | Self::GenericNotFoundError { .. } => StatusCode::NOT_FOUND, Self::ParameterUnknown { .. } | Self::HyperswitchUnprocessableEntity { .. } => { StatusCode::UNPROCESSABLE_ENTITY } diff --git a/crates/router/src/core/errors/api_error_response.rs b/crates/router/src/core/errors/api_error_response.rs index 19d1561d98..2154e07c27 100644 --- a/crates/router/src/core/errors/api_error_response.rs +++ b/crates/router/src/core/errors/api_error_response.rs @@ -196,6 +196,8 @@ pub enum ApiErrorResponse { MissingFilePurpose, #[error(error_type = ErrorType::InvalidRequestError, code = "HE_04", message = "File content type not found / valid")] MissingFileContentType, + #[error(error_type = ErrorType::InvalidRequestError, code = "HE_05", message = "{message}")] + GenericNotFoundError { message: String }, #[error(error_type = ErrorType::InvalidRequestError, code = "WE_01", message = "Failed to authenticate the webhook")] WebhookAuthenticationFailed, #[error(error_type = ErrorType::ObjectNotFound, code = "WE_04", message = "Webhook resource not found")] @@ -433,6 +435,9 @@ impl common_utils::errors::ErrorSwitch { AER::NotFound(ApiError::new("HE", 4, "Address does not exist in our records", None)) }, + Self::GenericNotFoundError { message } => { + AER::NotFound(ApiError::new("HE", 5, message, None)) + }, Self::ApiKeyNotFound => { AER::NotFound(ApiError::new("HE", 2, "API Key does not exist in our records", None)) } diff --git a/crates/router/src/db/queue.rs b/crates/router/src/db/queue.rs index ff246e1ec2..a5e08a125e 100644 --- a/crates/router/src/db/queue.rs +++ b/crates/router/src/db/queue.rs @@ -83,7 +83,9 @@ impl QueueInterface for Store { ttl: i64, ) -> CustomResult { let conn = self.redis_conn()?.clone(); - let is_lock_acquired = conn.set_key_if_not_exist(lock_key, lock_val).await; + let is_lock_acquired = conn + .set_key_if_not_exists_with_expiry(lock_key, lock_val, None) + .await; Ok(match is_lock_acquired { Ok(SetnxReply::KeySet) => match conn.set_expiry(lock_key, ttl).await { Ok(()) => true,