fix(error): propagate MissingRequiredFields api_error (#1244)

This commit is contained in:
Chethan Rao
2023-05-31 13:24:37 +05:30
committed by GitHub
parent 7032ea8494
commit 798881ab5b
3 changed files with 21 additions and 8 deletions

View File

@ -20,10 +20,7 @@ pub enum StripeErrorCode {
InvalidRequestUrl, InvalidRequestUrl,
#[error(error_type = StripeErrorType::InvalidRequestError, code = "parameter_missing", message = "Missing required param: {field_name}.")] #[error(error_type = StripeErrorType::InvalidRequestError, code = "parameter_missing", message = "Missing required param: {field_name}.")]
ParameterMissing { ParameterMissing { field_name: String, param: String },
field_name: &'static str,
param: &'static str,
},
#[error( #[error(
error_type = StripeErrorType::InvalidRequestError, code = "parameter_unknown", error_type = StripeErrorType::InvalidRequestError, code = "parameter_unknown",
@ -373,8 +370,15 @@ impl From<errors::ApiErrorResponse> for StripeErrorCode {
| errors::ApiErrorResponse::InvalidCardIinLength => Self::InvalidRequestUrl, | errors::ApiErrorResponse::InvalidCardIinLength => Self::InvalidRequestUrl,
errors::ApiErrorResponse::MissingRequiredField { field_name } => { errors::ApiErrorResponse::MissingRequiredField { field_name } => {
Self::ParameterMissing { Self::ParameterMissing {
field_name, field_name: field_name.to_string(),
param: field_name, param: field_name.to_string(),
}
}
errors::ApiErrorResponse::MissingRequiredFields { field_names } => {
// Instead of creating a new error variant in StripeErrorCode for MissingRequiredFields, converted vec<&str> to String
Self::ParameterMissing {
field_name: field_names.clone().join(", "),
param: field_names.clone().join(", "),
} }
} }
// parameter unknown, invalid request error // actually if we type wrong values in address we get this error. Stripe throws parameter unknown. I don't know if stripe is validating email and stuff // parameter unknown, invalid request error // actually if we type wrong values in address we get this error. Stripe throws parameter unknown. I don't know if stripe is validating email and stuff
@ -458,8 +462,8 @@ impl From<errors::ApiErrorResponse> for StripeErrorCode {
Self::PreconditionFailed { message } Self::PreconditionFailed { message }
} }
errors::ApiErrorResponse::InvalidDataValue { field_name } => Self::ParameterMissing { errors::ApiErrorResponse::InvalidDataValue { field_name } => Self::ParameterMissing {
field_name, field_name: field_name.to_string(),
param: field_name, param: field_name.to_string(),
}, },
errors::ApiErrorResponse::MaximumRefundCount => Self::MaximumRefundCount, errors::ApiErrorResponse::MaximumRefundCount => Self::MaximumRefundCount,
errors::ApiErrorResponse::PaymentNotSucceeded => Self::PaymentFailed, errors::ApiErrorResponse::PaymentNotSucceeded => Self::PaymentFailed,

View File

@ -87,6 +87,8 @@ pub enum ApiErrorResponse {
NotSupported { message: String }, NotSupported { message: String },
#[error(error_type = ErrorType::InvalidRequestError, code = "IR_20", message = "{flow} flow not supported by the {connector} connector")] #[error(error_type = ErrorType::InvalidRequestError, code = "IR_20", message = "{flow} flow not supported by the {connector} connector")]
FlowNotSupported { flow: String, connector: String }, FlowNotSupported { flow: String, connector: String },
#[error(error_type = ErrorType::InvalidRequestError, code = "IR_21", message = "Missing required params")]
MissingRequiredFields { field_names: Vec<&'static str> },
#[error(error_type = ErrorType::ConnectorError, code = "CE_00", message = "{code}: {message}", ignore = "status_code")] #[error(error_type = ErrorType::ConnectorError, code = "CE_00", message = "{code}: {message}", ignore = "status_code")]
ExternalConnectorError { ExternalConnectorError {
code: String, code: String,
@ -238,6 +240,7 @@ impl actix_web::ResponseError for ApiErrorResponse {
Self::InvalidRequestUrl | Self::WebhookResourceNotFound => StatusCode::NOT_FOUND, // 404 Self::InvalidRequestUrl | Self::WebhookResourceNotFound => StatusCode::NOT_FOUND, // 404
Self::InvalidHttpMethod => StatusCode::METHOD_NOT_ALLOWED, // 405 Self::InvalidHttpMethod => StatusCode::METHOD_NOT_ALLOWED, // 405
Self::MissingRequiredField { .. } Self::MissingRequiredField { .. }
| Self::MissingRequiredFields { .. }
| Self::InvalidDataValue { .. } | Self::InvalidDataValue { .. }
| Self::InvalidCardIin | Self::InvalidCardIin
| Self::InvalidCardIinLength => StatusCode::BAD_REQUEST, // 400 | Self::InvalidCardIinLength => StatusCode::BAD_REQUEST, // 400
@ -400,6 +403,9 @@ impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorRespon
19, 19,
"The provided client_secret has expired", None "The provided client_secret has expired", None
)), )),
Self::MissingRequiredFields { field_names } => AER::BadRequest(
ApiError::new("IR", 21, "Missing required params".to_string(), Some(Extra {data: Some(serde_json::json!(field_names)), ..Default::default() })),
),
Self::ExternalConnectorError { Self::ExternalConnectorError {
code, code,
message, message,

View File

@ -94,6 +94,9 @@ impl ConnectorErrorExt for error_stack::Report<errors::ConnectorError> {
errors::ConnectorError::MissingRequiredField { field_name } => { errors::ConnectorError::MissingRequiredField { field_name } => {
errors::ApiErrorResponse::MissingRequiredField { field_name } errors::ApiErrorResponse::MissingRequiredField { field_name }
} }
errors::ConnectorError::MissingRequiredFields { field_names } => {
errors::ApiErrorResponse::MissingRequiredFields { field_names: field_names.to_vec() }
}
errors::ConnectorError::NotImplemented(reason) => { errors::ConnectorError::NotImplemented(reason) => {
errors::ApiErrorResponse::NotImplemented { errors::ApiErrorResponse::NotImplemented {
message: errors::api_error_response::NotImplementedMessage::Reason( message: errors::api_error_response::NotImplementedMessage::Reason(