From 02a3ce74b84e86b0e17f8809c9b7651998a1c864 Mon Sep 17 00:00:00 2001 From: Abhishek Marrivagu <68317979+Abhicodes-crypto@users.noreply.github.com> Date: Tue, 20 Jun 2023 12:53:46 +0530 Subject: [PATCH] refactor(errors): refactor `actix_web::ResponseError` for `ApiErrorResponse` (#1362) --- .../src/core/errors/api_error_response.rs | 94 ++----------------- 1 file changed, 8 insertions(+), 86 deletions(-) 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::::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::::switch( + self, + ) + .error_response() } }