fix: Improve the error handling in Stripe collectCard method (#27249)
* fix: improve error handling in Stripe collectCard method - Add CollectCardFailure error code for better error categorization - Replace generic error with ErrorWithCode for consistent error handling - Add error mappings for common Stripe errors (customer not found, invalid API key, rate limit, etc.) - Include Stripe error message when available for better debugging - Follow the same pattern used in chargeCard method Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: add CollectCardFailure to getHttpStatusCode mapping This ensures the error returns 400 instead of 500 when collectCard fails. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * docs: add comments explaining ChargeCardFailure vs CollectCardFailure Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * refactor: simplify collectCard error handling Remove complex error mapping logic per code review feedback. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * test: add CollectCardFailure to HTTP status code tests Co-Authored-By: benny@cal.com <sldisek783@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -219,7 +219,8 @@ class StripePaymentService implements IAbstractPaymentService {
|
||||
bookingId,
|
||||
safeStringify(error)
|
||||
);
|
||||
throw new Error("Stripe: Payment method could not be collected");
|
||||
|
||||
throw new ErrorWithCode(ErrorCode.CollectCardFailure, "Stripe: Payment method could not be collected");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,10 @@ export enum ErrorCode {
|
||||
// Domain-specific error codes
|
||||
PaymentCreationFailure = "payment_not_created_error",
|
||||
NoAvailableUsersFound = "no_available_users_found_error",
|
||||
// Thrown when charging a card fails (e.g., insufficient funds, card declined during actual payment)
|
||||
ChargeCardFailure = "couldnt_charge_card_error",
|
||||
// Thrown when collecting/verifying a payment method fails (e.g., card setup failed during HOLD payment flow)
|
||||
CollectCardFailure = "couldnt_collect_card_error",
|
||||
RequestBodyWithouEnd = "request_body_end_time_internal_error",
|
||||
AlreadySignedUpForBooking = "already_signed_up_for_this_booking_error",
|
||||
FixedHostsUnavailableForBooking = "fixed_hosts_unavailable_for_booking",
|
||||
|
||||
@@ -20,6 +20,7 @@ const test400Codes = [
|
||||
ErrorCode.BookerLimitExceeded,
|
||||
ErrorCode.BookerLimitExceededReschedule,
|
||||
ErrorCode.ChargeCardFailure,
|
||||
ErrorCode.CollectCardFailure,
|
||||
];
|
||||
|
||||
const test404Codes = [
|
||||
|
||||
@@ -151,6 +151,7 @@ export function getHttpStatusCode(cause: Error | ErrorWithCode): number {
|
||||
case ErrorCode.EventTypeNoHosts:
|
||||
case ErrorCode.RequestBodyInvalid:
|
||||
case ErrorCode.ChargeCardFailure:
|
||||
case ErrorCode.CollectCardFailure:
|
||||
return 400;
|
||||
// 409 Conflict
|
||||
case ErrorCode.NoAvailableUsersFound:
|
||||
|
||||
Reference in New Issue
Block a user