fix(config): detect duplicate config insert and throw appropriate error (#1777)

This commit is contained in:
Nishant Joshi
2023-07-25 00:01:10 +05:30
committed by GitHub
parent 32c73243c0
commit 1ab4226c78
4 changed files with 11 additions and 1 deletions

View File

@ -79,6 +79,9 @@ pub enum StripeErrorCode {
#[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such config")] #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such config")]
ConfigNotFound, ConfigNotFound,
#[error(error_type = StripeErrorType::InvalidRequestError, code = "duplicate_resource", message = "Duplicate config")]
DuplicateConfig,
#[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such payment")] #[error(error_type = StripeErrorType::InvalidRequestError, code = "resource_missing", message = "No such payment")]
PaymentNotFound, PaymentNotFound,
@ -460,6 +463,7 @@ impl From<errors::ApiErrorResponse> for StripeErrorCode {
errors::ApiErrorResponse::MandateActive => Self::MandateActive, //not a stripe code errors::ApiErrorResponse::MandateActive => Self::MandateActive, //not a stripe code
errors::ApiErrorResponse::CustomerRedacted => Self::CustomerRedacted, //not a stripe code errors::ApiErrorResponse::CustomerRedacted => Self::CustomerRedacted, //not a stripe code
errors::ApiErrorResponse::ConfigNotFound => Self::ConfigNotFound, // not a stripe code errors::ApiErrorResponse::ConfigNotFound => Self::ConfigNotFound, // not a stripe code
errors::ApiErrorResponse::DuplicateConfig => Self::DuplicateConfig, // not a stripe code
errors::ApiErrorResponse::DuplicateRefundRequest => Self::DuplicateRefundRequest, errors::ApiErrorResponse::DuplicateRefundRequest => Self::DuplicateRefundRequest,
errors::ApiErrorResponse::DuplicatePayout { payout_id } => { errors::ApiErrorResponse::DuplicatePayout { payout_id } => {
Self::DuplicatePayout { payout_id } Self::DuplicatePayout { payout_id }
@ -575,6 +579,7 @@ impl actix_web::ResponseError for StripeErrorCode {
| Self::RefundNotFound | Self::RefundNotFound
| Self::CustomerNotFound | Self::CustomerNotFound
| Self::ConfigNotFound | Self::ConfigNotFound
| Self::DuplicateConfig
| Self::ClientSecretNotFound | Self::ClientSecretNotFound
| Self::PaymentNotFound | Self::PaymentNotFound
| Self::PaymentMethodNotFound | Self::PaymentMethodNotFound

View File

@ -17,7 +17,7 @@ pub async fn set_config(
config: config.value, config: config.value,
}) })
.await .await
.change_context(errors::ApiErrorResponse::InternalServerError) .to_duplicate_response(errors::ApiErrorResponse::DuplicateConfig)
.attach_printable("Unknown error, while setting config key")?; .attach_printable("Unknown error, while setting config key")?;
Ok(ApplicationResponse::Json(config.foreign_into())) Ok(ApplicationResponse::Json(config.foreign_into()))

View File

@ -137,6 +137,8 @@ pub enum ApiErrorResponse {
DuplicatePayment { payment_id: String }, DuplicatePayment { payment_id: String },
#[error(error_type = ErrorType::DuplicateRequest, code = "HE_01", message = "The payout with the specified payout_id '{payout_id}' already exists in our records")] #[error(error_type = ErrorType::DuplicateRequest, code = "HE_01", message = "The payout with the specified payout_id '{payout_id}' already exists in our records")]
DuplicatePayout { payout_id: String }, DuplicatePayout { payout_id: String },
#[error(error_type = ErrorType::DuplicateRequest, code = "HE_01", message = "The config with the specified key already exists in our records")]
DuplicateConfig,
#[error(error_type = ErrorType::ObjectNotFound, code = "HE_02", message = "Refund does not exist in our records")] #[error(error_type = ErrorType::ObjectNotFound, code = "HE_02", message = "Refund does not exist in our records")]
RefundNotFound, RefundNotFound,
#[error(error_type = ErrorType::ObjectNotFound, code = "HE_02", message = "Customer does not exist in our records")] #[error(error_type = ErrorType::ObjectNotFound, code = "HE_02", message = "Customer does not exist in our records")]

View File

@ -143,6 +143,9 @@ impl ErrorSwitch<api_models::errors::types::ApiErrorResponse> for ApiErrorRespon
} }
Self::ConfigNotFound => { Self::ConfigNotFound => {
AER::NotFound(ApiError::new("HE", 2, "Config key does not exist in our records.", None)) AER::NotFound(ApiError::new("HE", 2, "Config key does not exist in our records.", None))
},
Self::DuplicateConfig => {
AER::BadRequest(ApiError::new("HE", 1, "The config with the specified key already exists in our records", None))
} }
Self::PaymentNotFound => { Self::PaymentNotFound => {
AER::NotFound(ApiError::new("HE", 2, "Payment does not exist in our records", None)) AER::NotFound(ApiError::new("HE", 2, "Payment does not exist in our records", None))