Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions crates/router/src/connector/klarna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use transformers as klarna;
use crate::{
configs::settings,
connector::utils as connector_utils,
consts,
core::errors::{self, CustomResult},
headers,
services::{
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/connector/klarna/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,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>,
}