fix(connector): [Klarna] Handle error response with both error_messages and error_message fields (#1783)

This commit is contained in:
Arjun Karthik
2023-07-26 20:31:34 +05:30
committed by GitHub
parent 7f947169fe
commit 9cfdce0abe
2 changed files with 26 additions and 21 deletions

View File

@ -8,6 +8,7 @@ use transformers as klarna;
use crate::{
configs::settings,
connector::utils as connector_utils,
consts,
core::errors::{self, CustomResult},
headers,
services::{
@ -50,6 +51,27 @@ impl ConnectorCommon for Klarna {
auth.basic_token.into_masked(),
)])
}
fn build_error_response(
&self,
res: types::Response,
) -> CustomResult<types::ErrorResponse, errors::ConnectorError> {
let response: klarna::KlarnaErrorResponse = res
.response
.parse_struct("KlarnaErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
// KlarnaErrorResponse will either have error_messages or error_message field Ref: https://docs.klarna.com/api/errors/
let reason = response
.error_messages
.map(|messages| messages.join(" & "))
.or(response.error_message);
Ok(types::ErrorResponse {
status_code: res.status_code,
code: response.error_code,
message: consts::NO_ERROR_MESSAGE.to_string(),
reason,
})
}
}
impl api::Payment for Klarna {}
@ -174,16 +196,7 @@ impl
&self,
res: types::Response,
) -> CustomResult<types::ErrorResponse, errors::ConnectorError> {
let response: klarna::KlarnaErrorResponse = res
.response
.parse_struct("KlarnaErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
Ok(types::ErrorResponse {
status_code: res.status_code,
code: response.error_code,
message: response.error_messages.join(" & "),
reason: None,
})
self.build_error_response(res)
}
}
@ -338,16 +351,7 @@ impl
&self,
res: types::Response,
) -> CustomResult<types::ErrorResponse, errors::ConnectorError> {
let response: klarna::KlarnaErrorResponse = res
.response
.parse_struct("KlarnaErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
Ok(types::ErrorResponse {
status_code: res.status_code,
code: response.error_code,
message: response.error_messages.join(" & "),
reason: None,
})
self.build_error_response(res)
}
}

View File

@ -189,5 +189,6 @@ impl From<KlarnaFraudStatus> for enums::AttemptStatus {
#[derive(Deserialize)]
pub struct KlarnaErrorResponse {
pub error_code: String,
pub error_messages: Vec<String>,
pub error_messages: Option<Vec<String>>,
pub error_message: Option<String>,
}