Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(config): detect duplicate config insert and throw appropriate error (#1777)

Nishant Joshi1ab4226c780e
diff --git a/crates/router/src/compatibility/stripe/errors.rs b/crates/router/src/compatibility/stripe/errors.rs
index b9d711b195..feec71a37a 100644
--- a/crates/router/src/compatibility/stripe/errors.rs
+++ b/crates/router/src/compatibility/stripe/errors.rs
@@ -79,6 +79,9 @@ pub enum StripeErrorCode {
#[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such config")]
ConfigNotFound,
+ #[error(error_type = StripeErrorType::InvalidRequestError, code = "duplicate_resource", message = "Duplicate config")]
+ DuplicateConfig,
+
#[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such payment")]
PaymentNotFound,
@@ -460,6 +463,7 @@ impl From<errors::ApiErrorResponse> for StripeErrorCode {
errors::ApiErrorResponse::MandateActive => Self::MandateActive, //not a stripe code
errors::ApiErrorResponse::CustomerRedacted => Self::CustomerRedacted, //not a stripe code
errors::ApiErrorResponse::ConfigNotFound => Self::ConfigNotFound, // not a stripe code
+ errors::ApiErrorResponse::DuplicateConfig => Self::DuplicateConfig, // not a stripe code
errors::ApiErrorResponse::DuplicateRefundRequest => Self::DuplicateRefundRequest,
errors::ApiErrorResponse::DuplicatePayout { payout_id } => {
Self::DuplicatePayout { payout_id }
@@ -575,6 +579,7 @@ impl actix_web::ResponseError for StripeErrorCode {
| Self::RefundNotFound
| Self::CustomerNotFound
| Self::ConfigNotFound
+ | Self::DuplicateConfig
| Self::ClientSecretNotFound
| Self::PaymentNotFound
| Self::PaymentMethodNotFound
diff --git a/crates/router/src/core/configs.rs b/crates/router/src/core/configs.rs
index 3d7afbd548..b17d8a1499 100644
--- a/crates/router/src/core/configs.rs
+++ b/crates/router/src/core/configs.rs
@@ -17,7 +17,7 @@ pub async fn set_config(
config: config.value,
})
.await
- .change_context(errors::ApiErrorResponse::InternalServerError)
+ .to_duplicate_response(errors::ApiErrorResponse::DuplicateConfig)
.attach_printable("Unknown error, while setting config key")?;
Ok(ApplicationResponse::Json(config.foreign_into()))
diff --git a/crates/router/src/core/errors/api_error_response.rs b/crates/router/src/core/errors/api_error_response.rs
index 70f2d982b0..450eb52a94 100644
--- a/crates/router/src/core/errors/api_error_response.rs
+++ b/crates/router/src/core/errors/api_error_response.rs
@@ -137,6 +137,8 @@ pub enum ApiErrorResponse {
DuplicatePayment { payment_id: String },
#[error(error_type = ErrorType::DuplicateRequest, code = "HE_01", message = "The payout with the specified payout_id '{payout_id}' already exists in our records")]
DuplicatePayout { payout_id: String },
+ #[error(error_type = ErrorType::DuplicateRequest, code = "HE_01", message = "The config with the specified key already exists in our records")]
+ DuplicateConfig,
#[error(error_type = ErrorType::ObjectNotFound, code = "HE_02", message = "Refund does not exist in our records")]
RefundNotFound,
#[error(error_type = ErrorType::ObjectNotFound, code = "HE_02", message = "Customer does not exist in our records")]
diff --git a/crates/router/src/core/errors/transformers.rs b/crates/router/src/core/errors/transformers.rs
index 7e2afab018..a315240b0d 100644
--- a/crates/router/src/core/errors/transformers.rs
+++ b/crates/router/src/core/errors/transformers.rs
@@ -143,6 +143,9 @@ impl ErrorSwitch<api_models::errors::types::ApiErrorResponse> for ApiErrorRespon
}
Self::ConfigNotFound => {
AER::NotFound(ApiError::new("HE", 2, "Config key does not exist in our records.", None))
+ },
+ Self::DuplicateConfig => {
+ AER::BadRequest(ApiError::new("HE", 1, "The config with the specified key already exists in our records", None))
}
Self::PaymentNotFound => {
AER::NotFound(ApiError::new("HE", 2, "Payment does not exist in our records", None))