refactor(connector): update error handling for Nexinets, Cybersource (#1151)

Signed-off-by: chikke srujan <121822803+srujanchikke@users.noreply.github.com>
This commit is contained in:
chikke srujan
2023-05-30 03:18:35 +05:30
committed by GitHub
parent 22b2fa3061
commit 2ede8ade8c
6 changed files with 93 additions and 68 deletions

View File

@ -93,22 +93,30 @@ impl ConnectorCommon for Cybersource {
.response
.parse_struct("Cybersource ErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
let details = response.details.unwrap_or(vec![]);
let connector_reason = details
.iter()
.map(|det| format!("{} : {}", det.field, det.reason))
.collect::<Vec<_>>()
.join(", ");
let (code, message) = match response.error_information {
Some(ref error_info) => (error_info.reason.clone(), error_info.message.clone()),
None => (
response
.reason
.map_or(consts::NO_ERROR_CODE.to_string(), |reason| {
reason.to_string()
}),
response
.message
.map_or(consts::NO_ERROR_MESSAGE.to_string(), |message| message),
),
};
Ok(types::ErrorResponse {
status_code: res.status_code,
code: consts::NO_ERROR_CODE.to_string(),
message: response
.message
.map(|m| {
format!(
"{} {}",
m,
response.details.map(|d| d.to_string()).unwrap_or_default()
)
.trim()
.to_string()
})
.unwrap_or_else(|| consts::NO_ERROR_MESSAGE.to_string()),
reason: response.reason,
code,
message,
reason: Some(connector_reason),
})
}
}

View File

@ -388,14 +388,41 @@ impl<F, T>
}
}
#[derive(Debug, Default, Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ErrorResponse {
pub error_information: Option<ErrorInformation>,
pub status: Option<String>,
pub message: Option<String>,
pub reason: Option<String>,
pub details: Option<serde_json::Value>,
pub reason: Option<Reason>,
pub details: Option<Vec<Details>>,
}
#[derive(Debug, Deserialize, strum::Display)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Reason {
MissingField,
InvalidData,
DuplicateRequest,
InvalidCard,
AuthAlreadyReversed,
CardTypeNotAccepted,
InvalidMerchantConfiguration,
ProcessorUnavailable,
InvalidAmount,
InvalidCardType,
InvalidPaymentId,
NotSupported,
SystemError,
ServerTimeout,
ServiceTimeout,
}
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Details {
pub field: String,
pub reason: String,
}
#[derive(Debug, Default, Deserialize)]

View File

@ -97,6 +97,7 @@ impl ConnectorCommon for Nexinets {
let errors = response.errors.clone();
let mut message = String::new();
let mut static_message = String::new();
for error in errors.iter() {
let field = error.field.to_owned().unwrap_or_default();
let mut msg = String::new();
@ -107,16 +108,18 @@ impl ConnectorCommon for Nexinets {
}
if message.is_empty() {
message.push_str(&msg);
static_message.push_str(&msg);
} else {
message.push_str(format!(", {}", msg).as_str());
}
}
let connector_reason = format!("reason : {} , message : {}", response.message, message);
Ok(ErrorResponse {
status_code: response.status,
code: response.code.to_string(),
message,
reason: Some(response.message),
message: static_message,
reason: Some(connector_reason),
})
}
}