Skip to content

Commit dbfe529

Browse files
committed
DRY error handling for variations
1 parent 1796b20 commit dbfe529

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

Modules/Sources/Yosemite/Tools/POS/POSOrderService.swift

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,34 @@ private extension POSOrderService {
143143
_ error: Error,
144144
cart: POSCart
145145
) -> [CartOrderComparison.MissingCartItem]? {
146-
// Check if this is an AFError wrapping a DotcomError or NetworkError
147-
let underlyingError: Error? = {
148-
if let afError = error as? AFError {
149-
return afError.underlyingError
146+
let (code, data) = errorCodeAndData(from: error)
147+
guard let code, isProductValidationError(code: code) else {
148+
return nil
149+
}
150+
151+
guard let variationID = extractVariationID(from: data) else {
152+
return unknownMissingProductsInCart()
153+
}
154+
return createMissingProductInfo(forVariationID: variationID, cart: cart)
155+
}
156+
157+
func errorCodeAndData(from error: Error) -> (code: String?, data: [String: AnyDecodable]?) {
158+
// Unwrap AFError if present
159+
let underlyingError: Error = {
160+
if let afError = error as? AFError, let underlying = afError.underlyingError {
161+
return underlying
150162
}
151163
return error
152164
}()
153165

154-
// Check for DotcomError with product/variation validation error codes
166+
// Extract error code and data from either error type
155167
if case .unknown(let code, _, let data) = underlyingError as? DotcomError {
156-
if isProductValidationError(code: code) {
157-
if let variationID = extractVariationID(from: data) {
158-
return createMissingProductInfo(forVariationID: variationID, cart: cart)
159-
}
160-
return extractMissingProductsFromCart()
161-
}
168+
return (code, data)
162169
}
163-
164-
// Check for NetworkError with product/variation validation error codes
165-
if let networkError = underlyingError as? NetworkError,
166-
let errorCode = networkError.errorCode,
167-
isProductValidationError(code: errorCode) {
168-
// Try to extract variation_id from NetworkError data if available
169-
if let variationID = extractVariationID(from: networkError.errorData) {
170-
return createMissingProductInfo(forVariationID: variationID, cart: cart)
171-
}
172-
// Fall back to generic error if no variation_id in data
173-
return extractMissingProductsFromCart()
170+
if let networkError = underlyingError as? NetworkError {
171+
return (networkError.errorCode, networkError.errorData)
174172
}
175-
176-
return nil
173+
return (nil, nil)
177174
}
178175

179176
/// Extracts variation_id from error data dictionary
@@ -239,7 +236,7 @@ private extension POSOrderService {
239236

240237
/// Extracts missing products by trying to identify items in cart that might have caused the validation error
241238
/// Since server doesn't tell us which specific products failed, we return generic error info
242-
func extractMissingProductsFromCart() -> [CartOrderComparison.MissingCartItem]? {
239+
func unknownMissingProductsInCart() -> [CartOrderComparison.MissingCartItem] {
243240
// We can't determine which specific products are invalid from the server error
244241
// So we return a generic missing product message with 0 for both IDs (meaning unknown)
245242
// The user will need to remove all products and retry

0 commit comments

Comments
 (0)