Skip to content

Commit a513e09

Browse files
refactor: simplify error names - nest error in CheckoutError
1 parent 27bf5f1 commit a513e09

File tree

4 files changed

+82
-46
lines changed

4 files changed

+82
-46
lines changed

Samples/MobileBuyIntegration/MobileBuyIntegration/ViewControllers/CartViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ extension CartViewController: CheckoutDelegate {
603603
}
604604
}
605605

606-
func checkoutDidFail(error: ShopifyCheckoutSheetKit.CheckoutError) {
606+
func checkoutDidFail(error: CheckoutError) {
607607
var errorMessage = ""
608608

609609
/// Internal Checkout SDK error
@@ -634,7 +634,7 @@ extension CartViewController: CheckoutDelegate {
634634
}
635635
}
636636

637-
private func handleCheckoutUnavailable(_ message: String, _ code: CheckoutUnavailable) {
637+
private func handleCheckoutUnavailable(_ message: String, _ code: CheckoutError.CheckoutUnavailable) {
638638
switch code {
639639
case let .clientError(clientErrorCode):
640640
print("[CheckoutUnavailable] (checkoutError)", message, clientErrorCode)

Sources/ShopifyCheckoutSheetKit/CheckoutError.swift

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,33 @@
2323

2424
import Foundation
2525

26-
public enum CheckoutErrorCode: String, Codable {
27-
case invalidPayload = "INVALID_PAYLOAD"
28-
case invalidSignature = "INVALID_SIGNATURE"
29-
case notAuthorized = "NOT_AUTHORIZED"
30-
case payloadExpired = "PAYLOAD_EXPIRED"
31-
case customerAccountRequired = "CUSTOMER_ACCOUNT_REQUIRED"
32-
case storefrontPasswordRequired = "STOREFRONT_PASSWORD_REQUIRED"
33-
case cartCompleted = "CART_COMPLETED"
34-
case invalidCart = "INVALID_CART"
35-
case killswitchEnabled = "KILLSWITCH_ENABLED"
36-
case unrecoverableFailure = "UNRECOVERABLE_FAILURE"
37-
case policyViolation = "POLICY_VIOLATION"
38-
case vaultedPaymentError = "VAULTED_PAYMENT_ERROR"
39-
case checkoutLiquidNotMigrated = "CHECKOUT_LIQUID_NOT_MIGRATED"
40-
}
41-
42-
public enum CheckoutUnavailable {
43-
case clientError(code: CheckoutErrorCode)
44-
case httpError(statusCode: Int)
45-
}
46-
47-
/// A type representing Shopify Checkout specific errors.
48-
/// "recoverable" indicates that though the request has failed, it should be retried in a fallback browser experience.
49-
public enum CheckoutError: Swift.Error {
26+
/// `CheckoutError` represents scenarios where Shopify Checkout may error
27+
///
28+
/// Each error relates to a different portion of Web Shopify Checkout, except `.sdk` which is an internal swift error
29+
/// When the error is not `.sdk` it is useful to first confirm where the issue exists in your Storefront
30+
/// within a browser, to exclude Checkout Kit from the investigation
31+
///
32+
/// Every event has a "recoverable" property that indicates this error may be recoverable when retried in a fallback browser experience
33+
/// This may have a degraded experience, implement CheckoutDelegate.shouldRecoverFromError to opt out
34+
public enum CheckoutError: Error {
5035
/// Issued when an internal error within Shopify Checkout SDK
5136
/// In event of an sdkError you could use the stacktrace to inform you of how to proceed,
52-
/// if the issue persists, it is recommended to open a bug report in http://github.com/Shopify/checkout-sheet-kit-swift
53-
case sdk(underlying: Swift.Error, recoverable: Bool = true)
37+
/// if the issue persists, it is recommended to open a bug report in:
38+
/// http://github.com/Shopify/checkout-sheet-kit-swift/issues
39+
case sdk(underlying: Error, recoverable: Bool = true)
5440

5541
/// Issued when the storefront configuration has caused an error.
56-
/// Note that the Checkout Sheet Kit only supports stores migrated for extensibility.
57-
case misconfiguration(message: String, code: CheckoutErrorCode, recoverable: Bool = false)
42+
case misconfiguration(message: String, code: ErrorCode, recoverable: Bool = false)
5843

5944
/// Issued when checkout has encountered a unrecoverable error (for example server side error)
60-
/// if the issue persists, it is recommended to open a bug report in http://github.com/Shopify/checkout-sheet-kit-swift
45+
/// if the issue persists, it is recommended to open a bug report:
46+
/// http://github.com/Shopify/checkout-sheet-kit-swift/issues
6147
case unavailable(message: String, code: CheckoutUnavailable, recoverable: Bool)
6248

6349
/// Issued when checkout is no longer available and will no longer be available with the checkout url supplied.
6450
/// This may happen when the user has paused on checkout for a long period (hours) and then attempted to proceed again with the same checkout url
6551
/// In event of checkoutExpired, a new checkout url will need to be generated
66-
case expired(message: String, code: CheckoutErrorCode, recoverable: Bool = false)
52+
case expired(message: String, code: ErrorCode, recoverable: Bool = false)
6753

6854
public var isRecoverable: Bool {
6955
switch self {
@@ -74,15 +60,32 @@ public enum CheckoutError: Swift.Error {
7460
return recoverable
7561
}
7662
}
77-
}
7863

79-
public struct CheckoutErrorEvent: CheckoutNotification {
80-
public static let method = "checkout.error"
81-
public let code: CheckoutErrorCode
82-
public let message: String
64+
public enum ErrorCode: String, Codable {
65+
/// misconfiguration: recoverable:false
66+
case payloadExpired = "PAYLOAD_EXPIRED"
67+
case invalidPayload = "INVALID_PAYLOAD"
68+
case invalidSignature = "INVALID_SIGNATURE"
69+
case notAuthorized = "NOT_AUTHORIZED"
70+
case customerAccountRequired = "CUSTOMER_ACCOUNT_REQUIRED"
71+
case storefrontPasswordRequired = "STOREFRONT_PASSWORD_REQUIRED"
72+
73+
/// unavailable: recoverable:true
74+
case killswitchEnabled = "KILLSWITCH_ENABLED"
75+
case unrecoverableFailure = "UNRECOVERABLE_FAILURE"
76+
case policyViolation = "POLICY_VIOLATION"
77+
case vaultedPaymentError = "VAULTED_PAYMENT_ERROR"
78+
79+
/// expired: recoverable:false
80+
case cartCompleted = "CART_COMPLETED"
81+
case invalidCart = "INVALID_CART"
82+
83+
/// deprecated?
84+
case checkoutLiquidNotMigrated = "CHECKOUT_LIQUID_NOT_MIGRATED"
85+
}
8386

84-
public init(code: CheckoutErrorCode, message: String) {
85-
self.code = code
86-
self.message = message
87+
public enum CheckoutUnavailable {
88+
case clientError(code: ErrorCode)
89+
case httpError(statusCode: Int)
8790
}
8891
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
MIT License
3+
4+
Copyright 2023 - Present, Shopify Inc.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
public struct CheckoutErrorEvent: CheckoutNotification {
25+
public static let method = "checkout.error"
26+
public let code: CheckoutError.ErrorCode
27+
public let message: String
28+
29+
public init(code: CheckoutError.ErrorCode, message: String) {
30+
self.code = code
31+
self.message = message
32+
}
33+
}

Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ extension CheckoutWebView: WKScriptMessageHandler {
363363
viewDelegate.checkoutViewDidFailWithError(
364364
error: .unavailable(
365365
message: errorEvent.message,
366-
code: CheckoutUnavailable.clientError(code: errorEvent.code),
366+
code: .clientError(code: errorEvent.code),
367367
recoverable: true
368368
))
369369
}
@@ -433,7 +433,7 @@ extension CheckoutWebView: WKNavigationDelegate {
433433
viewDelegate?.checkoutViewDidFailWithError(
434434
error: .unavailable(
435435
message: errorMessageForStatusCode,
436-
code: CheckoutUnavailable.httpError(statusCode: statusCode),
436+
code: CheckoutError.CheckoutUnavailable.httpError(statusCode: statusCode),
437437
recoverable: false
438438
))
439439
case 404:
@@ -451,30 +451,30 @@ extension CheckoutWebView: WKNavigationDelegate {
451451
viewDelegate?.checkoutViewDidFailWithError(
452452
error: .unavailable(
453453
message: errorMessageForStatusCode,
454-
code: CheckoutUnavailable.httpError(statusCode: statusCode),
454+
code: .httpError(statusCode: statusCode),
455455
recoverable: false
456456
))
457457
}
458458
case 410:
459459
OSLogger.shared.debug("Gone (410)")
460460
viewDelegate?.checkoutViewDidFailWithError(
461461
error: .expired(
462-
message: "Checkout has expired.", code: CheckoutErrorCode.invalidCart
462+
message: "Checkout has expired.", code: CheckoutError.ErrorCode.invalidCart
463463
))
464464
case 500 ... 599:
465465
OSLogger.shared.debug("Server error (5xx)")
466466
viewDelegate?.checkoutViewDidFailWithError(
467467
error: .unavailable(
468468
message: errorMessageForStatusCode,
469-
code: CheckoutUnavailable.httpError(statusCode: statusCode),
469+
code: .httpError(statusCode: statusCode),
470470
recoverable: allowRecoverable
471471
))
472472
default:
473473
OSLogger.shared.debug("\(statusCode) error received")
474474
viewDelegate?.checkoutViewDidFailWithError(
475475
error: .unavailable(
476476
message: errorMessageForStatusCode,
477-
code: CheckoutUnavailable.httpError(statusCode: statusCode),
477+
code: .httpError(statusCode: statusCode),
478478
recoverable: false
479479
))
480480
}

0 commit comments

Comments
 (0)