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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(connector): [zift] handle error responses with missing fields (#14873)
Akshaya Shankar0d6514a3d414
diff --git a/crates/common_utils/src/ext_traits.rs b/crates/common_utils/src/ext_traits.rsindex dfe14154fe..08f4367455 100644--- a/crates/common_utils/src/ext_traits.rs+++ b/crates/common_utils/src/ext_traits.rs@@ -226,6 +226,10 @@ pub trait ValueExt {fn parse_value<T>(self, type_name: &'static str) -> CustomResult<T, errors::ParsingError>whereT: serde::de::DeserializeOwned;++ /// Replace every leaf value with its datatype name, keeping the structure intact+ /// e.g. `{"abc": "123"}` becomes `{"abc": "String"}`+ fn to_type_schema(&self) -> serde_json::Value;}impl ValueExt for serde_json::Value {@@ -243,6 +247,21 @@ impl ValueExt for serde_json::Value {)})}++ fn to_type_schema(&self) -> serde_json::Value {+ match self {+ Self::Null => Self::String("Null".to_string()),+ Self::Bool(_) => Self::String("Boolean".to_string()),+ Self::Number(_) => Self::String("Number".to_string()),+ Self::String(_) => Self::String("String".to_string()),+ Self::Array(values) => Self::Array(values.iter().map(Self::to_type_schema).collect()),+ Self::Object(map) => Self::Object(+ map.iter()+ .map(|(key, value)| (key.clone(), value.to_type_schema()))+ .collect(),+ ),+ }+ }}impl<MaskingStrategy> ValueExt for Secret<serde_json::Value, MaskingStrategy>@@ -255,6 +274,10 @@ where{self.expose().parse_value(type_name)}++ fn to_type_schema(&self) -> serde_json::Value {+ self.peek().to_type_schema()+ }}impl<E: ValueExt + Clone> ValueExt for crypto::Encryptable<E> {@@ -264,6 +287,10 @@ impl<E: ValueExt + Clone> ValueExt for crypto::Encryptable<E> {{self.into_inner().parse_value(type_name)}++ fn to_type_schema(&self) -> serde_json::Value {+ self.get_inner().to_type_schema()+ }}/// Extending functionalities of `String` for performing parsingdiff --git a/crates/hyperswitch_connectors/src/connectors/zift.rs b/crates/hyperswitch_connectors/src/connectors/zift.rsindex 4073a04551..d4082ba71c 100644--- a/crates/hyperswitch_connectors/src/connectors/zift.rs+++ b/crates/hyperswitch_connectors/src/connectors/zift.rs@@ -5,6 +5,7 @@ use std::sync::LazyLock;use common_enums::enums;use common_utils::{errors::CustomResult,+ ext_traits::ValueExt,request::{Method, Request, RequestBuilder, RequestContent},types::{AmountConvertor, StringMinorUnit, StringMinorUnitForConnector},};@@ -36,6 +37,7 @@ use hyperswitch_interfaces::{ConnectorValidation,},configs::Connectors,+ consts::{NO_ERROR_CODE, NO_ERROR_MESSAGE},errors,events::connector_api_logs::ConnectorEvent,types::{self, Response},@@ -203,14 +205,44 @@ impl ConnectorCommon for Zift {let response: zift::ZiftErrorResponse = serde_urlencoded::from_bytes(&res.response).change_context(errors::ConnectorError::ResponseDeserializationFailed)?;+ if res.response.is_empty() {+ router_env::logger::info!(+ "Zift connector returned an empty response body for error response"+ );+ } else if response.response_code.is_none()+ && response.response_message.is_none()+ && response.failure_code.is_none()+ && response.failure_message.is_none()+ {+ let value = serde_json::from_slice::<serde_json::Value>(&res.response)+ .ok()+ .or_else(|| serde_urlencoded::from_bytes::<serde_json::Value>(&res.response).ok())+ .ok_or_else(|| report!(errors::ConnectorError::ResponseDeserializationFailed))+ .attach_printable("Failed to parse the Zift error response")?;+ router_env::logger::info!(+ "Zift connector returned an unexpected error response with structure: {}",+ value.to_type_schema()+ );+ }+event_builder.map(|i| i.set_response_body(&response));router_env::logger::info!(connector_response=?response);+ let error_message = response+ .response_message+ .clone()+ .or(response.failure_message.clone());+Ok(ErrorResponse {status_code: res.status_code,- code: response.response_code,- message: response.response_message.clone(),- reason: Some(response.response_message),+ code: response+ .response_code+ .or(response.failure_code)+ .unwrap_or(NO_ERROR_CODE.to_string()),+ message: error_message+ .clone()+ .unwrap_or(NO_ERROR_MESSAGE.to_string()),+ reason: error_message,attempt_status: None,connector_transaction_id: None,connector_response_reference_id: None,diff --git a/crates/hyperswitch_connectors/src/connectors/zift/transformers.rs b/crates/hyperswitch_connectors/src/connectors/zift/transformers.rsindex 023446b04e..02a6bc39de 100644--- a/crates/hyperswitch_connectors/src/connectors/zift/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/zift/transformers.rs@@ -395,10 +395,10 @@ impl ResponseCodeExt for String {#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]#[serde(rename_all = "camelCase")]pub struct ZiftErrorResponse {- pub response_code: String,- pub response_message: String,- pub failure_code: String,- pub failure_message: String,+ pub response_code: Option<String>,+ pub response_message: Option<String>,+ pub failure_code: Option<String>,+ pub failure_message: Option<String>,}#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]