refactor(connector): update error handling for Paypal, Checkout, Mollie to include detailed messages (#1150)

This commit is contained in:
Prasunna Soppa
2023-05-16 13:29:05 +05:30
committed by GitHub
parent da4d721424
commit e044c2fd9a
6 changed files with 75 additions and 103 deletions

View File

@ -92,17 +92,18 @@ impl ConnectorCommon for Checkout {
.parse_struct("ErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?
};
Ok(types::ErrorResponse {
status_code: res.status_code,
code: response
.error_codes
.unwrap_or_else(|| vec![consts::NO_ERROR_CODE.to_string()])
.join(" & "),
message: response
.error_type
.clone()
.unwrap_or_else(|| consts::NO_ERROR_CODE.to_string()),
message: response
.error_codes
.as_ref()
.and_then(|error_codes| error_codes.first().cloned())
.unwrap_or_else(|| consts::NO_ERROR_MESSAGE.to_string()),
reason: None,
reason: response.error_codes.map(|errors| errors.join(" & ")),
})
}
}

View File

@ -7,6 +7,7 @@ use transformers as mollie;
use crate::{
configs::settings,
consts,
core::{
errors::{self, CustomResult},
payments,
@ -82,10 +83,12 @@ impl ConnectorCommon for Mollie {
.parse_struct("MollieErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
Ok(ErrorResponse {
status_code: response.status,
code: response
.title
.unwrap_or_else(|| consts::NO_ERROR_CODE.to_string()),
message: response.detail,
reason: response.field,
status_code: response.status,
..Default::default()
})
}
}

View File

@ -71,37 +71,32 @@ impl Paypal {
.parse_struct("Paypal ErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
let message = match response.details {
Some(mes) => {
let mut des = "".to_owned();
for item in mes.iter() {
let mut description = format!("description - {}", item.to_owned().description);
if let Some(data) = &item.value {
description.push_str(format!(", value - {}", data.to_owned()).as_str());
let error_reason = match response.details {
Some(order_errors) => order_errors
.iter()
.map(|error| {
let mut reason = format!("description - {}", error.description);
if let Some(value) = &error.value {
reason.push_str(&format!(", value - {value}"));
}
if let Some(data) = &item.field {
let field = data
.clone()
.split('/')
.last()
.unwrap_or_default()
.to_owned();
description.push_str(format!(", field - {};", field).as_str());
if let Some(field) = error
.field
.as_ref()
.and_then(|field| field.split('/').last())
{
reason.push_str(&format!(", field - {field}"));
}
des.push_str(description.as_str())
}
des
}
reason.push(';');
reason
})
.collect::<String>(),
None => consts::NO_ERROR_MESSAGE.to_string(),
};
Ok(ErrorResponse {
status_code: res.status_code,
code: response.name,
message,
reason: None,
message: response.message,
reason: Some(error_reason),
})
}
}
@ -168,24 +163,18 @@ impl ConnectorCommon for Paypal {
.parse_struct("Paypal ErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
let message = match response.details {
Some(mes) => {
let mut des = "".to_owned();
for item in mes.iter() {
let x = item.clone().description;
let st = format!("description - {} ; ", x);
des.push_str(&st);
}
des
}
let error_reason = match response.details {
Some(error_details) => error_details
.iter()
.map(|error| format!("description - {} ; ", error.description))
.collect::<String>(),
None => consts::NO_ERROR_MESSAGE.to_string(),
};
Ok(ErrorResponse {
status_code: res.status_code,
code: response.name,
message,
reason: None,
message: response.message,
reason: Some(error_reason),
})
}
}