Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw bad request while pushing duplicate data to redis #3016

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/router/src/core/errors/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,25 @@ impl<T> ConnectorErrorExt<T> for error_stack::Result<T, errors::ConnectorError>
}
}
}

pub trait RedisErrorExt {
#[track_caller]
fn to_redis_failed_response(self, key: &str) -> error_stack::Report<errors::StorageError>;
}

impl RedisErrorExt for error_stack::Report<errors::RedisError> {
fn to_redis_failed_response(self, key: &str) -> error_stack::Report<errors::StorageError> {
match self.current_context() {
errors::RedisError::NotFound => self.change_context(
errors::StorageError::ValueNotFound(format!("Data does not exist for key {key}",)),
),
errors::RedisError::SetNxFailed => {
self.change_context(errors::StorageError::DuplicateValue {
entity: "redis",
key: Some(key.to_string()),
})
}
_ => self.change_context(errors::StorageError::KVError),
}
}
}
6 changes: 3 additions & 3 deletions crates/router/src/db/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ mod storage {
use super::RefundInterface;
use crate::{
connection,
core::errors::{self, CustomResult},
core::errors::{self, utils::RedisErrorExt, CustomResult},
db::reverse_lookup::ReverseLookupInterface,
logger,
services::Store,
Expand Down Expand Up @@ -392,7 +392,7 @@ mod storage {
&key,
)
.await
.change_context(errors::StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&key))?
.try_into_hsetnx()
{
Ok(HsetnxReply::KeyNotSet) => Err(errors::StorageError::DuplicateValue {
Expand Down Expand Up @@ -550,7 +550,7 @@ mod storage {
&key,
)
.await
.change_context(errors::StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&key))?
.try_into_hset()
.change_context(errors::StorageError::KVError)?;

Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/db/reverse_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod storage {
use super::{ReverseLookupInterface, Store};
use crate::{
connection,
core::errors::utils::RedisErrorExt,
errors::{self, CustomResult},
types::storage::{
enums, kv,
Expand Down Expand Up @@ -109,7 +110,7 @@ mod storage {
format!("reverse_lookup_{}", &created_rev_lookup.lookup_id),
)
.await
.change_context(errors::StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&created_rev_lookup.lookup_id))?
.try_into_setnx()
{
Ok(SetnxReply::KeySet) => Ok(created_rev_lookup),
Expand Down
8 changes: 6 additions & 2 deletions crates/router/src/utils/db_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{core::errors, routes::metrics};
use crate::{
core::errors::{self, utils::RedisErrorExt},
routes::metrics,
};

/// Generates hscan field pattern. Suppose the field is pa_1234_ref_1211 it will generate
/// pa_1234_ref_*
Expand Down Expand Up @@ -28,7 +31,8 @@ where
metrics::KV_MISS.add(&metrics::CONTEXT, 1, &[]);
database_call_closure().await
}
_ => Err(redis_error.change_context(errors::StorageError::KVError)),
// Keeping the key empty here since the error would never go here.
_ => Err(redis_error.to_redis_failed_response("")),
},
}
}
20 changes: 20 additions & 0 deletions crates/storage_impl/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ impl StorageError {
}
}

pub trait RedisErrorExt {
#[track_caller]
fn to_redis_failed_response(self, key: &str) -> error_stack::Report<DataStorageError>;
}

impl RedisErrorExt for error_stack::Report<RedisError> {
fn to_redis_failed_response(self, key: &str) -> error_stack::Report<DataStorageError> {
match self.current_context() {
RedisError::NotFound => self.change_context(DataStorageError::ValueNotFound(format!(
"Data does not exist for key {key}",
))),
RedisError::SetNxFailed => self.change_context(DataStorageError::DuplicateValue {
entity: "redis",
key: Some(key.to_string()),
}),
_ => self.change_context(DataStorageError::KVError),
}
}
}

impl_error_type!(EncryptionError, "Encryption error");

#[derive(Debug, thiserror::Error)]
Expand Down
3 changes: 2 additions & 1 deletion crates/storage_impl/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use redis_interface::SetnxReply;

use crate::{
diesel_error_to_data_error,
errors::RedisErrorExt,
redis::kv_store::{kv_wrapper, KvOperation},
utils::{self, try_redis_get_else_try_database_get},
DatabaseStore, KVRouterStore, RouterStore,
Expand Down Expand Up @@ -97,7 +98,7 @@ impl<T: DatabaseStore> ReverseLookupInterface for KVRouterStore<T> {
format!("reverse_lookup_{}", &created_rev_lookup.lookup_id),
)
.await
.change_context(errors::StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&created_rev_lookup.lookup_id))?
.try_into_setnx()
{
Ok(SetnxReply::KeySet) => Ok(created_rev_lookup),
Expand Down
3 changes: 2 additions & 1 deletion crates/storage_impl/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use router_env::{instrument, tracing};

use crate::{
diesel_error_to_data_error,
errors::RedisErrorExt,
lookup::ReverseLookupInterface,
redis::kv_store::{kv_wrapper, KvOperation},
utils::{pg_connection_read, pg_connection_write, try_redis_get_else_try_database_get},
Expand Down Expand Up @@ -409,7 +410,7 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
&key,
)
.await
.change_context(errors::StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&key))?
.try_into_hsetnx()
{
Ok(HsetnxReply::KeyNotSet) => Err(errors::StorageError::DuplicateValue {
Expand Down
5 changes: 3 additions & 2 deletions crates/storage_impl/src/payments/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use router_env::{instrument, tracing};
use crate::connection;
use crate::{
diesel_error_to_data_error,
errors::RedisErrorExt,
redis::kv_store::{kv_wrapper, KvOperation},
utils::{self, pg_connection_read, pg_connection_write},
DataModelExt, DatabaseStore, KVRouterStore,
Expand Down Expand Up @@ -116,7 +117,7 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
&key,
)
.await
.change_context(StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&key))?
.try_into_hsetnx()
{
Ok(HsetnxReply::KeyNotSet) => Err(StorageError::DuplicateValue {
Expand Down Expand Up @@ -177,7 +178,7 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
&key,
)
.await
.change_context(StorageError::KVError)?
.map_err(|err| err.to_redis_failed_response(&key))?
.try_into_hset()
.change_context(StorageError::KVError)?;

Expand Down
5 changes: 3 additions & 2 deletions crates/storage_impl/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use data_models::errors::StorageError;
use diesel::PgConnection;
use error_stack::{IntoReport, ResultExt};

use crate::{metrics, DatabaseStore};
use crate::{errors::RedisErrorExt, metrics, DatabaseStore};

pub async fn pg_connection_read<T: DatabaseStore>(
store: &T,
Expand Down Expand Up @@ -64,7 +64,8 @@ where
metrics::KV_MISS.add(&metrics::CONTEXT, 1, &[]);
database_call_closure().await
}
_ => Err(redis_error.change_context(StorageError::KVError)),
// Keeping the key empty here since the error would never go here.
_ => Err(redis_error.to_redis_failed_response("")),
},
}
}
Loading