From 9fe20932157e9700cf723c662e54b579feee908c Mon Sep 17 00:00:00 2001 From: Narayan Bhat <48803246+Narayanbhat166@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:46:27 +0530 Subject: [PATCH] bugfix: use connector error handler for 500 error messages. (#696) --- crates/api_models/src/enums.rs | 1 + .../src/connector/adyen/transformers.rs | 1 + crates/router/src/connector/klarna.rs | 38 ++++++++++--------- .../src/connector/stripe/transformers.rs | 11 ++++-- .../src/connector/worldline/transformers.rs | 10 ++++- crates/router/src/core/errors.rs | 1 + .../src/core/errors/api_error_response.rs | 2 +- crates/router/src/core/errors/utils.rs | 4 +- crates/router/src/services/api.rs | 24 ++++++++---- crates/router/tests/connectors/worldline.rs | 3 +- 10 files changed, 60 insertions(+), 35 deletions(-) diff --git a/crates/api_models/src/enums.rs b/crates/api_models/src/enums.rs index c3125080f3..5c51c6d5ff 100644 --- a/crates/api_models/src/enums.rs +++ b/crates/api_models/src/enums.rs @@ -356,6 +356,7 @@ pub enum PaymentMethodIssuerCode { Debug, serde::Serialize, serde::Deserialize, + strum::Display, ToSchema, Default, frunk::LabelledGeneric, diff --git a/crates/router/src/connector/adyen/transformers.rs b/crates/router/src/connector/adyen/transformers.rs index 6d589d5d3e..71123ac1f6 100644 --- a/crates/router/src/connector/adyen/transformers.rs +++ b/crates/router/src/connector/adyen/transformers.rs @@ -397,6 +397,7 @@ impl<'a> TryFrom<&api_enums::BankNames> for AdyenTestBankNames<'a> { _ => Err(errors::ConnectorError::NotSupported { payment_method: String::from("BankRedirect"), connector: "Adyen", + payment_experience: api_enums::PaymentExperience::RedirectToUrl.to_string(), })?, }) } diff --git a/crates/router/src/connector/klarna.rs b/crates/router/src/connector/klarna.rs index 9781e19b0d..56d23b1652 100644 --- a/crates/router/src/connector/klarna.rs +++ b/crates/router/src/connector/klarna.rs @@ -7,6 +7,7 @@ use transformers as klarna; use crate::{ configs::settings, + connector::utils as connector_utils, core::errors::{self, CustomResult}, headers, services::{self}, @@ -230,34 +231,37 @@ impl connectors: &settings::Connectors, ) -> CustomResult { let payment_method_data = &req.request.payment_method_data; + let payment_experience = req + .request + .payment_experience + .as_ref() + .ok_or_else(connector_utils::missing_field_err("payment_experience"))?; + let payment_method_type = req + .request + .payment_method_type + .as_ref() + .ok_or_else(connector_utils::missing_field_err("payment_method_type"))?; + match payment_method_data { api_payments::PaymentMethodData::PayLater(api_payments::PayLaterData::KlarnaSdk { token, - }) => match ( - req.request.payment_experience.as_ref(), - req.request.payment_method_type.as_ref(), - ) { + }) => match (payment_experience, payment_method_type) { ( - Some(storage_enums::PaymentExperience::InvokeSdkClient), - Some(storage_enums::PaymentMethodType::Klarna), + storage_enums::PaymentExperience::InvokeSdkClient, + storage_enums::PaymentMethodType::Klarna, ) => Ok(format!( "{}payments/v1/authorizations/{}/order", self.base_url(connectors), token )), - (None, _) | (_, None) => Err(error_stack::report!( - errors::ConnectorError::MissingRequiredField { - field_name: "payment_experience/payment_method_type" - } - )), - _ => Err(error_stack::report!( - errors::ConnectorError::MismatchedPaymentData - )), + _ => Err(error_stack::report!(errors::ConnectorError::NotSupported { + payment_method: payment_method_type.to_string(), + connector: "klarna", + payment_experience: payment_experience.to_string() + })), }, _ => Err(error_stack::report!( - errors::ConnectorError::NotImplemented( - "We only support wallet payments through klarna".to_string(), - ) + errors::ConnectorError::MismatchedPaymentData )), } } diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs index d31dadc985..09ac25fecd 100644 --- a/crates/router/src/connector/stripe/transformers.rs +++ b/crates/router/src/connector/stripe/transformers.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use api_models::{self, payments}; +use api_models::{self, enums as api_enums, payments}; use common_utils::{fp_utils, pii::Email}; use error_stack::{IntoReport, ResultExt}; use masking::ExposeInterface; @@ -315,8 +315,9 @@ impl TryFrom<&api_models::enums::BankNames> for StripeBankNames { api_models::enums::BankNames::VolkskreditbankAg => Self::VolkskreditbankAg, api_models::enums::BankNames::VrBankBraunau => Self::VrBankBraunau, _ => Err(errors::ConnectorError::NotSupported { - payment_method: String::from("BankRedirect"), + payment_method: api_enums::PaymentMethod::BankRedirect.to_string(), connector: "Stripe", + payment_experience: api_enums::PaymentExperience::RedirectToUrl.to_string(), })?, }) } @@ -366,14 +367,16 @@ fn infer_stripe_pay_later_type( Ok(StripePaymentMethodType::AfterpayClearpay) } _ => Err(errors::ConnectorError::NotSupported { - payment_method: format!("{pm_type} payments by {experience}"), + payment_method: pm_type.to_string(), connector: "stripe", + payment_experience: experience.to_string(), }), } } else { Err(errors::ConnectorError::NotSupported { - payment_method: format!("{pm_type} payments by {experience}"), + payment_method: pm_type.to_string(), connector: "stripe", + payment_experience: experience.to_string(), }) } } diff --git a/crates/router/src/connector/worldline/transformers.rs b/crates/router/src/connector/worldline/transformers.rs index bd6a9d063e..5cb928e631 100644 --- a/crates/router/src/connector/worldline/transformers.rs +++ b/crates/router/src/connector/worldline/transformers.rs @@ -6,7 +6,12 @@ use serde::{Deserialize, Serialize}; use crate::{ connector::utils::{self, CardData}, core::errors, - types::{self, api, storage::enums, transformers::ForeignFrom}, + types::{ + self, + api::{self, enums as api_enums}, + storage::enums, + transformers::ForeignFrom, + }, }; #[derive(Default, Debug, Serialize, Eq, PartialEq)] @@ -125,8 +130,9 @@ impl TryFrom for Gateway { utils::CardIssuer::Discover => Ok(Self::Discover), utils::CardIssuer::Visa => Ok(Self::Visa), _ => Err(errors::ConnectorError::NotSupported { - payment_method: format!("{issuer}"), + payment_method: api_enums::PaymentMethod::Card.to_string(), connector: "worldline", + payment_experience: api_enums::PaymentExperience::RedirectToUrl.to_string(), } .into()), } diff --git a/crates/router/src/core/errors.rs b/crates/router/src/core/errors.rs index c4eaf3653b..1c9fe12b22 100644 --- a/crates/router/src/core/errors.rs +++ b/crates/router/src/core/errors.rs @@ -254,6 +254,7 @@ pub enum ConnectorError { NotSupported { payment_method: String, connector: &'static str, + payment_experience: String, }, #[error("Missing connector transaction ID")] MissingConnectorTransactionID, diff --git a/crates/router/src/core/errors/api_error_response.rs b/crates/router/src/core/errors/api_error_response.rs index a176f42e18..5aab0ce345 100644 --- a/crates/router/src/core/errors/api_error_response.rs +++ b/crates/router/src/core/errors/api_error_response.rs @@ -423,7 +423,7 @@ impl common_utils::errors::ErrorSwitch { - AER::BadRequest(ApiError::new("HE", 3, "{message}", None)) + AER::BadRequest(ApiError::new("HE", 3, "Payment method type not supported", Some(Extra {reason: Some(message.to_owned()), ..Default::default()}))) } } } diff --git a/crates/router/src/core/errors/utils.rs b/crates/router/src/core/errors/utils.rs index c0ee87e9a0..a0e951fc3a 100644 --- a/crates/router/src/core/errors/utils.rs +++ b/crates/router/src/core/errors/utils.rs @@ -107,8 +107,8 @@ impl ConnectorErrorExt for error_stack::Report { "payment_method_data, payment_method_type and payment_experience does not match", } }, - errors::ConnectorError::NotSupported { payment_method, connector } => { - errors::ApiErrorResponse::NotSupported { message: format!("{payment_method} is not supported by {connector}") } + errors::ConnectorError::NotSupported { payment_method, connector, payment_experience } => { + errors::ApiErrorResponse::NotSupported { message: format!("Payment method type {payment_method} is not supported by {connector} through payment experience {payment_experience}") } } _ => errors::ApiErrorResponse::InternalServerError, }; diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs index 9962d0237d..f7065633b1 100644 --- a/crates/router/src/services/api.rs +++ b/crates/router/src/services/api.rs @@ -315,14 +315,22 @@ async fn handle_response( } status_code @ 500..=599 => { - let error = match status_code { - 500 => errors::ApiClientError::InternalServerErrorReceived, - 502 => errors::ApiClientError::BadGatewayReceived, - 503 => errors::ApiClientError::ServiceUnavailableReceived, - 504 => errors::ApiClientError::GatewayTimeoutReceived, - _ => errors::ApiClientError::UnexpectedServerResponse, - }; - Err(Report::new(error).attach_printable("Server error response received")) + let bytes = response.bytes().await.map_err(|error| { + report!(error) + .change_context(errors::ApiClientError::ResponseDecodingFailed) + .attach_printable("Client error response received") + })?; + // let error = match status_code { + // 500 => errors::ApiClientError::InternalServerErrorReceived, + // 502 => errors::ApiClientError::BadGatewayReceived, + // 503 => errors::ApiClientError::ServiceUnavailableReceived, + // 504 => errors::ApiClientError::GatewayTimeoutReceived, + // _ => errors::ApiClientError::UnexpectedServerResponse, + // }; + Ok(Err(types::Response { + response: bytes, + status_code, + })) } status_code @ 400..=499 => { diff --git a/crates/router/tests/connectors/worldline.rs b/crates/router/tests/connectors/worldline.rs index d70faaa424..0932123add 100644 --- a/crates/router/tests/connectors/worldline.rs +++ b/crates/router/tests/connectors/worldline.rs @@ -135,7 +135,8 @@ async fn should_throw_not_implemented_for_unsupported_issuer() { *response.unwrap_err().current_context(), errors::ConnectorError::NotSupported { payment_method: "Maestro".to_string(), - connector: "worldline" + connector: "worldline", + payment_experience: "redirect_to_url".to_string(), } ) }