feat: add GenericNotFoundError error response and set_key_if_not_exists_with_expiry Redis command (#1526)

This commit is contained in:
Prajjwal Kumar
2023-07-03 18:21:23 +05:30
committed by GitHub
parent 1e87f3d673
commit 9a88a32d50
5 changed files with 22 additions and 6 deletions

View File

@ -16,7 +16,7 @@ pub async fn is_stream_available(stream_index: u8, store: Arc<services::Store>)
match store match store
.redis_conn .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 .await
{ {
Ok(resp) => resp == redis::types::SetnxReply::KeySet, Ok(resp) => resp == redis::types::SetnxReply::KeySet,

View File

@ -175,10 +175,11 @@ impl super::RedisConnectionPool {
} }
#[instrument(level = "DEBUG", skip(self))] #[instrument(level = "DEBUG", skip(self))]
pub async fn set_key_if_not_exist<V>( pub async fn set_key_if_not_exists_with_expiry<V>(
&self, &self,
key: &str, key: &str,
value: V, value: V,
seconds: Option<i64>,
) -> CustomResult<SetnxReply, errors::RedisError> ) -> CustomResult<SetnxReply, errors::RedisError>
where where
V: TryInto<RedisValue> + Debug + Send + Sync, V: TryInto<RedisValue> + Debug + Send + Sync,
@ -188,9 +189,11 @@ impl super::RedisConnectionPool {
.set( .set(
key, key,
value, value,
Some(Expiration::EX(self.config.default_ttl.into())), Some(Expiration::EX(
seconds.unwrap_or(self.config.default_ttl.into()),
)),
Some(SetOptions::NX), Some(SetOptions::NX),
false, true,
) )
.await .await
.into_report() .into_report()

View File

@ -82,6 +82,9 @@ pub enum StripeErrorCode {
#[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such payment method")] #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such payment method")]
PaymentMethodNotFound, 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")] #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such merchant account")]
MerchantAccountNotFound, MerchantAccountNotFound,
@ -391,6 +394,9 @@ impl From<errors::ApiErrorResponse> for StripeErrorCode {
param: field_names.clone().join(", "), 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 // 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 { errors::ApiErrorResponse::InvalidDataFormat {
field_name, field_name,
@ -535,7 +541,7 @@ impl actix_web::ResponseError for StripeErrorCode {
match self { match self {
Self::Unauthorized => StatusCode::UNAUTHORIZED, Self::Unauthorized => StatusCode::UNAUTHORIZED,
Self::InvalidRequestUrl => StatusCode::NOT_FOUND, Self::InvalidRequestUrl | Self::GenericNotFoundError { .. } => StatusCode::NOT_FOUND,
Self::ParameterUnknown { .. } | Self::HyperswitchUnprocessableEntity { .. } => { Self::ParameterUnknown { .. } | Self::HyperswitchUnprocessableEntity { .. } => {
StatusCode::UNPROCESSABLE_ENTITY StatusCode::UNPROCESSABLE_ENTITY
} }

View File

@ -196,6 +196,8 @@ pub enum ApiErrorResponse {
MissingFilePurpose, MissingFilePurpose,
#[error(error_type = ErrorType::InvalidRequestError, code = "HE_04", message = "File content type not found / valid")] #[error(error_type = ErrorType::InvalidRequestError, code = "HE_04", message = "File content type not found / valid")]
MissingFileContentType, 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")] #[error(error_type = ErrorType::InvalidRequestError, code = "WE_01", message = "Failed to authenticate the webhook")]
WebhookAuthenticationFailed, WebhookAuthenticationFailed,
#[error(error_type = ErrorType::ObjectNotFound, code = "WE_04", message = "Webhook resource not found")] #[error(error_type = ErrorType::ObjectNotFound, code = "WE_04", message = "Webhook resource not found")]
@ -433,6 +435,9 @@ impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorRespon
Self::AddressNotFound => { Self::AddressNotFound => {
AER::NotFound(ApiError::new("HE", 4, "Address does not exist in our records", None)) 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 => { Self::ApiKeyNotFound => {
AER::NotFound(ApiError::new("HE", 2, "API Key does not exist in our records", None)) AER::NotFound(ApiError::new("HE", 2, "API Key does not exist in our records", None))
} }

View File

@ -83,7 +83,9 @@ impl QueueInterface for Store {
ttl: i64, ttl: i64,
) -> CustomResult<bool, RedisError> { ) -> CustomResult<bool, RedisError> {
let conn = self.redis_conn()?.clone(); 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(match is_lock_acquired {
Ok(SetnxReply::KeySet) => match conn.set_expiry(lock_key, ttl).await { Ok(SetnxReply::KeySet) => match conn.set_expiry(lock_key, ttl).await {
Ok(()) => true, Ok(()) => true,