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

refactor(payments): attempt to address unintended 5xx and 4xx in payments (#1376)

Abhishek Marrivagucf64862daca0
diff --git a/crates/router/src/core/customers.rs b/crates/router/src/core/customers.rs
index 508c4045ab..cc51d9316d 100644
--- a/crates/router/src/core/customers.rs
+++ b/crates/router/src/core/customers.rs
@@ -5,7 +5,6 @@ use common_utils::{
use error_stack::ResultExt;
use masking::ExposeInterface;
use router_env::{instrument, tracing};
-use storage_models::errors as storage_errors;
use crate::{
consts,
@@ -225,13 +224,15 @@ pub async fn delete_customer(
.to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?;
}
}
- Err(error) => match error.current_context() {
- errors::StorageError::DatabaseError(err) => match err.current_context() {
- storage_errors::DatabaseError::NotFound => Ok(()),
- _ => Err(errors::ApiErrorResponse::InternalServerError),
- },
- _ => Err(errors::ApiErrorResponse::InternalServerError),
- }?,
+ Err(error) => {
+ if error.current_context().is_db_not_found() {
+ Ok(())
+ } else {
+ Err(error)
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("failed find_payment_method_by_customer_id_merchant_id_list")
+ }?
+ }
};
let key = types::get_merchant_enc_key(&**db, merchant_account.merchant_id.clone())
@@ -266,13 +267,15 @@ pub async fn delete_customer(
.await
{
Ok(_) => Ok(()),
- Err(error) => match error.current_context() {
- errors::StorageError::DatabaseError(err) => match err.current_context() {
- storage_errors::DatabaseError::NotFound => Ok(()),
- _ => Err(errors::ApiErrorResponse::InternalServerError),
- },
- _ => Err(errors::ApiErrorResponse::InternalServerError),
- },
+ Err(error) => {
+ if error.current_context().is_db_not_found() {
+ Ok(())
+ } else {
+ Err(error)
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("failed update_address_by_merchant_id_customer_id")
+ }
+ }
}?;
let updated_customer = storage::CustomerUpdate::Update {
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index f8643e2321..89f7333f43 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -26,6 +26,7 @@ use self::{
helpers::authenticate_client_secret,
operations::{payment_complete_authorize, BoxedOperation, Operation},
};
+use super::errors::StorageErrorExt;
use crate::{
configs::settings::PaymentMethodTypeTokenFilter,
core::{
@@ -104,7 +105,7 @@ where
validate_result.merchant_id,
)
.await
- .change_context(errors::ApiErrorResponse::InternalServerError)
+ .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)
.attach_printable("Failed while fetching/creating customer")?;
let connector = get_connector_choice(
@@ -1090,7 +1091,7 @@ pub async fn list_payments(
) -> RouterResponse<api::PaymentListResponse> {
use futures::stream::StreamExt;
- use crate::{core::errors::utils::StorageErrorExt, types::transformers::ForeignFrom};
+ use crate::types::transformers::ForeignFrom;
helpers::validate_payment_list_request(&constraints)?;
let merchant_id = &merchant.merchant_id;
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 80944c1970..11249c5b63 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -935,7 +935,7 @@ pub async fn create_customer_if_not_exist<'a, F: Clone, R>(
None => None,
Some(customer_id) => db
.find_customer_optional_by_customer_id_merchant_id(customer_id, merchant_id)
- .await?
+ .await? // if customer_id is present in payment_intent but not found in customer table then shouldn't an error be thrown ?
.map(Ok),
},
};
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index ff7f0220b2..88680aece5 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -12,7 +12,7 @@ use crate::{
configs::settings,
core::{
api_keys,
- errors::{self, RouterResult},
+ errors::{self, utils::StorageErrorExt, RouterResult},
},
db::StorageInterface,
routes::app::AppStateInfo,
@@ -122,13 +122,7 @@ where
.store()
.find_merchant_account_by_merchant_id(&stored_api_key.merchant_id)
.await
- .map_err(|e| {
- if e.current_context().is_db_not_found() {
- e.change_context(errors::ApiErrorResponse::Unauthorized)
- } else {
- e.change_context(errors::ApiErrorResponse::InternalServerError)
- }
- })
+ .to_not_found_response(errors::ApiErrorResponse::Unauthorized)
}
}