From 0c33c24b7fdf4b7343633ea5d4e570c0249a0b35 Mon Sep 17 00:00:00 2001 From: DEEPANSHU BANSAL Date: Mon, 20 Nov 2023 18:53:29 +0530 Subject: [PATCH 1/2] fix(connector): [CASHTOCODE] Fix Error Response Handling --- crates/router/src/connector/cashtocode.rs | 13 ++++- .../src/connector/cashtocode/transformers.rs | 47 ++++++++++++++----- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/crates/router/src/connector/cashtocode.rs b/crates/router/src/connector/cashtocode.rs index a8d7d6d8050..274d4c59af7 100644 --- a/crates/router/src/connector/cashtocode.rs +++ b/crates/router/src/connector/cashtocode.rs @@ -114,10 +114,19 @@ impl ConnectorCommon for Cashtocode { .response .parse_struct("CashtocodeErrorResponse") .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; + let (code, message) = match response { + cashtocode::CashtocodeErrorResponse::StringError(string_error_data) => { + (string_error_data.error, string_error_data.error_description) + } + cashtocode::CashtocodeErrorResponse::IntError(int_error_data) => ( + int_error_data.error.to_string(), + int_error_data.error_description, + ), + }; Ok(ErrorResponse { status_code: res.status_code, - code: response.error.to_string(), - message: response.error_description, + code, + message, reason: None, attempt_status: None, }) diff --git a/crates/router/src/connector/cashtocode/transformers.rs b/crates/router/src/connector/cashtocode/transformers.rs index 2caef69db92..7e0f9c25c21 100644 --- a/crates/router/src/connector/cashtocode/transformers.rs +++ b/crates/router/src/connector/cashtocode/transformers.rs @@ -210,16 +210,27 @@ impl >, ) -> Result { let (status, response) = match item.response { - CashtocodePaymentsResponse::CashtoCodeError(error_data) => ( - enums::AttemptStatus::Failure, - Err(types::ErrorResponse { - code: error_data.error.to_string(), - status_code: item.http_code, - message: error_data.error_description, - reason: None, - attempt_status: None, - }), - ), + CashtocodePaymentsResponse::CashtoCodeError(error_data) => { + let (code, message) = match error_data { + CashtocodeErrorResponse::StringError(string_error_data) => { + (string_error_data.error, string_error_data.error_description) + } + CashtocodeErrorResponse::IntError(int_error_data) => ( + int_error_data.error.to_string(), + int_error_data.error_description, + ), + }; + ( + enums::AttemptStatus::Failure, + Err(types::ErrorResponse { + code, + status_code: item.http_code, + message, + reason: None, + attempt_status: None, + }), + ) + } CashtocodePaymentsResponse::CashtoCodeData(response_data) => { let redirection_data = services::RedirectForm::Form { endpoint: response_data.pay_url, @@ -288,12 +299,26 @@ impl } #[derive(Debug, Deserialize)] -pub struct CashtocodeErrorResponse { +#[serde(untagged)] +pub enum CashtocodeErrorResponse { + StringError(CashtocodeStringErrorResponse), + IntError(CashtocodeIntErrorResponse), +} + +#[derive(Debug, Deserialize)] +pub struct CashtocodeStringErrorResponse { pub error: String, pub error_description: String, pub errors: Option>, } +#[derive(Debug, Deserialize)] +pub struct CashtocodeIntErrorResponse { + pub error: i64, + pub error_description: String, + pub errors: Option>, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct CashtocodeIncomingWebhook { From a84cc33c5606b5bf58d8978d05b8782c7f3677c8 Mon Sep 17 00:00:00 2001 From: DEEPANSHU BANSAL Date: Mon, 20 Nov 2023 19:24:21 +0530 Subject: [PATCH 2/2] fix(connector):[CASHTOCODE] Resolve PR Comments --- crates/router/src/connector/cashtocode.rs | 13 +---- .../src/connector/cashtocode/transformers.rs | 49 +++++-------------- 2 files changed, 14 insertions(+), 48 deletions(-) diff --git a/crates/router/src/connector/cashtocode.rs b/crates/router/src/connector/cashtocode.rs index 274d4c59af7..a8d7d6d8050 100644 --- a/crates/router/src/connector/cashtocode.rs +++ b/crates/router/src/connector/cashtocode.rs @@ -114,19 +114,10 @@ impl ConnectorCommon for Cashtocode { .response .parse_struct("CashtocodeErrorResponse") .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; - let (code, message) = match response { - cashtocode::CashtocodeErrorResponse::StringError(string_error_data) => { - (string_error_data.error, string_error_data.error_description) - } - cashtocode::CashtocodeErrorResponse::IntError(int_error_data) => ( - int_error_data.error.to_string(), - int_error_data.error_description, - ), - }; Ok(ErrorResponse { status_code: res.status_code, - code, - message, + code: response.error.to_string(), + message: response.error_description, reason: None, attempt_status: None, }) diff --git a/crates/router/src/connector/cashtocode/transformers.rs b/crates/router/src/connector/cashtocode/transformers.rs index 7e0f9c25c21..42e47c077e8 100644 --- a/crates/router/src/connector/cashtocode/transformers.rs +++ b/crates/router/src/connector/cashtocode/transformers.rs @@ -210,27 +210,16 @@ impl >, ) -> Result { let (status, response) = match item.response { - CashtocodePaymentsResponse::CashtoCodeError(error_data) => { - let (code, message) = match error_data { - CashtocodeErrorResponse::StringError(string_error_data) => { - (string_error_data.error, string_error_data.error_description) - } - CashtocodeErrorResponse::IntError(int_error_data) => ( - int_error_data.error.to_string(), - int_error_data.error_description, - ), - }; - ( - enums::AttemptStatus::Failure, - Err(types::ErrorResponse { - code, - status_code: item.http_code, - message, - reason: None, - attempt_status: None, - }), - ) - } + CashtocodePaymentsResponse::CashtoCodeError(error_data) => ( + enums::AttemptStatus::Failure, + Err(types::ErrorResponse { + code: error_data.error.to_string(), + status_code: item.http_code, + message: error_data.error_description, + reason: None, + attempt_status: None, + }), + ), CashtocodePaymentsResponse::CashtoCodeData(response_data) => { let redirection_data = services::RedirectForm::Form { endpoint: response_data.pay_url, @@ -299,22 +288,8 @@ impl } #[derive(Debug, Deserialize)] -#[serde(untagged)] -pub enum CashtocodeErrorResponse { - StringError(CashtocodeStringErrorResponse), - IntError(CashtocodeIntErrorResponse), -} - -#[derive(Debug, Deserialize)] -pub struct CashtocodeStringErrorResponse { - pub error: String, - pub error_description: String, - pub errors: Option>, -} - -#[derive(Debug, Deserialize)] -pub struct CashtocodeIntErrorResponse { - pub error: i64, +pub struct CashtocodeErrorResponse { + pub error: serde_json::Value, pub error_description: String, pub errors: Option>, }