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

This commit is contained in:
Abhishek Marrivagu
2023-06-20 12:53:46 +05:30
committed by GitHub
parent 5d515050cf
commit 02a3ce74b8

View File

@ -236,95 +236,17 @@ impl ::core::fmt::Display for ApiErrorResponse {
impl actix_web::ResponseError for ApiErrorResponse { impl actix_web::ResponseError for ApiErrorResponse {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
match self { common_utils::errors::ErrorSwitch::<api_models::errors::types::ApiErrorResponse>::switch(
Self::Unauthorized self,
| Self::InvalidEphemeralKey )
| Self::InvalidJwtToken .status_code()
| 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,
}
} }
fn error_response(&self) -> actix_web::HttpResponse { fn error_response(&self) -> actix_web::HttpResponse {
use actix_web::http::header; common_utils::errors::ErrorSwitch::<api_models::errors::types::ApiErrorResponse>::switch(
self,
actix_web::HttpResponseBuilder::new(self.status_code()) )
.insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON)) .error_response()
.body(self.to_string())
} }
} }