Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(errors): refactor `actix_web::ResponseError` for `ApiErrorResponse` (#1362)

Abhishek Marrivagu02a3ce74b84e
diff --git a/crates/router/src/core/errors/api_error_response.rs b/crates/router/src/core/errors/api_error_response.rs
index 9387719d36..75f12b7ec6 100644
--- a/crates/router/src/core/errors/api_error_response.rs
+++ b/crates/router/src/core/errors/api_error_response.rs
@@ -236,95 +236,17 @@ impl ::core::fmt::Display for ApiErrorResponse {
impl actix_web::ResponseError for ApiErrorResponse {
fn status_code(&self) -> StatusCode {
- match self {
- Self::Unauthorized
- | Self::InvalidEphemeralKey
- | Self::InvalidJwtToken
- | Self::GenericUnauthorized { .. }
- | Self::WebhookAuthenticationFailed => StatusCode::UNAUTHORIZED, // 401
- Self::ExternalConnectorError { status_code, .. } => {
- StatusCode::from_u16(*status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
- }
- Self::AccessForbidden => StatusCode::FORBIDDEN, // 403
- Self::InvalidRequestUrl | Self::WebhookResourceNotFound => StatusCode::NOT_FOUND, // 404
- Self::InvalidHttpMethod => StatusCode::METHOD_NOT_ALLOWED, // 405
- Self::MissingRequiredField { .. }
- | Self::MissingRequiredFields { .. }
- | Self::InvalidDataValue { .. }
- | Self::InvalidCardIin
- | Self::InvalidCardIinLength => StatusCode::BAD_REQUEST, // 400
- Self::InvalidDataFormat { .. } | Self::InvalidRequestData { .. } => {
- StatusCode::UNPROCESSABLE_ENTITY
- } // 422
-
- Self::PaymentAuthorizationFailed { .. }
- | Self::PaymentAuthenticationFailed { .. }
- | Self::PaymentCaptureFailed { .. }
- | Self::InvalidCardData { .. }
- | Self::CardExpired { .. }
- | Self::RefundFailed { .. }
- | Self::RefundNotPossible { .. }
- | Self::VerificationFailed { .. }
- | Self::PaymentUnexpectedState { .. }
- | Self::MandateValidationFailed { .. }
- | Self::DisputeFailed { .. }
- | Self::RefundAmountExceedsPaymentAmount
- | Self::MaximumRefundCount
- | Self::IncorrectPaymentMethodConfiguration
- | Self::PreconditionFailed { .. } => StatusCode::BAD_REQUEST, // 400
-
- Self::MandateUpdateFailed
- | Self::InternalServerError
- | Self::WebhookProcessingFailure => StatusCode::INTERNAL_SERVER_ERROR, // 500
- Self::DuplicateRefundRequest | Self::DuplicatePayment { .. } => StatusCode::BAD_REQUEST, // 400
- Self::RefundNotFound
- | Self::CustomerNotFound
- | Self::MandateActive
- | Self::CustomerRedacted
- | Self::PaymentNotFound
- | Self::PaymentMethodNotFound
- | Self::MerchantAccountNotFound
- | Self::MerchantConnectorAccountNotFound { .. }
- | Self::MerchantConnectorAccountDisabled
- | Self::MandateNotFound
- | Self::ClientSecretNotGiven
- | Self::ClientSecretExpired
- | Self::ClientSecretInvalid
- | Self::SuccessfulPaymentNotFound
- | Self::IncorrectConnectorNameGiven
- | Self::ResourceIdNotFound
- | Self::ConfigNotFound
- | Self::AddressNotFound
- | Self::NotSupported { .. }
- | Self::FlowNotSupported { .. }
- | Self::ApiKeyNotFound
- | Self::DisputeStatusValidationFailed { .. }
- | Self::WebhookBadRequest => StatusCode::BAD_REQUEST, // 400
- Self::DuplicateMerchantAccount
- | Self::DuplicateMerchantConnectorAccount { .. }
- | Self::DuplicatePaymentMethod
- | Self::DuplicateMandate
- | Self::DisputeNotFound { .. }
- | Self::MissingFile
- | Self::FileValidationFailed { .. }
- | Self::MissingFileContentType
- | Self::MissingFilePurpose
- | Self::MissingDisputeId
- | Self::FileNotFound
- | Self::FileNotAvailable => StatusCode::BAD_REQUEST, // 400
- Self::ReturnUrlUnavailable => StatusCode::SERVICE_UNAVAILABLE, // 503
- Self::PaymentNotSucceeded => StatusCode::BAD_REQUEST, // 400
- Self::NotImplemented { .. } => StatusCode::NOT_IMPLEMENTED, // 501
- Self::WebhookUnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY,
- }
+ common_utils::errors::ErrorSwitch::<api_models::errors::types::ApiErrorResponse>::switch(
+ self,
+ )
+ .status_code()
}
fn error_response(&self) -> actix_web::HttpResponse {
- use actix_web::http::header;
-
- actix_web::HttpResponseBuilder::new(self.status_code())
- .insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON))
- .body(self.to_string())
+ common_utils::errors::ErrorSwitch::<api_models::errors::types::ApiErrorResponse>::switch(
+ self,
+ )
+ .error_response()
}
}