@@ -21,16 +21,118 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222*/
2323
24+ /**
25+ * Error codes that can be returned from checkout errors.
26+ */
2427export enum CheckoutErrorCode {
28+ // ============================================================================
29+ // Configuration errors
30+ // ============================================================================
31+
32+ /**
33+ * The app authentication payload passed could not be decoded.
34+ */
35+ invalidPayload = 'invalid_payload' ,
36+
37+ /**
38+ * The app authentication JWT signature or encrypted token signature was invalid.
39+ */
40+ invalidSignature = 'invalid_signature' ,
41+
42+ /**
43+ * The app authentication access token was not valid for the shop.
44+ */
45+ notAuthorized = 'not_authorized' ,
46+
47+ /**
48+ * The provided app authentication payload has expired.
49+ */
50+ payloadExpired = 'payload_expired' ,
51+
52+ /**
53+ * The buyer must be logged in to a customer account to proceed with checkout.
54+ */
55+ customerAccountRequired = 'customer_account_required' ,
56+
57+ /**
58+ * The storefront requires a password to access checkout.
59+ */
2560 storefrontPasswordRequired = 'storefront_password_required' ,
26- cartExpired = 'cart_expired' ,
61+
62+ // ============================================================================
63+ // Cart errors
64+ // ============================================================================
65+
66+ /**
67+ * The cart associated with the checkout has already been completed.
68+ */
2769 cartCompleted = 'cart_completed' ,
70+
71+ /**
72+ * The cart is invalid or no longer exists.
73+ */
2874 invalidCart = 'invalid_cart' ,
75+
76+ // ============================================================================
77+ // Client errors
78+ // ============================================================================
79+
80+ /**
81+ * Checkout preloading has been temporarily disabled via killswitch.
82+ */
83+ killswitchEnabled = 'killswitch_enabled' ,
84+
85+ /**
86+ * An unrecoverable error occurred during checkout.
87+ */
88+ unrecoverableFailure = 'unrecoverable_failure' ,
89+
90+ /**
91+ * A policy violation was detected during checkout.
92+ */
93+ policyViolation = 'policy_violation' ,
94+
95+ /**
96+ * An error occurred processing a vaulted payment method.
97+ */
98+ vaultedPaymentError = 'vaulted_payment_error' ,
99+
100+ // ============================================================================
101+ // Internal errors
102+ // ============================================================================
103+
104+ /**
105+ * A client-side error occurred in the SDK.
106+ */
29107 clientError = 'client_error' ,
108+
109+ /**
110+ * An HTTP error occurred while communicating with the checkout.
111+ */
30112 httpError = 'http_error' ,
113+
114+ /**
115+ * Failed to send a message to the checkout bridge.
116+ */
31117 sendingBridgeEventError = 'error_sending_message' ,
118+
119+ /**
120+ * Failed to receive a message from the checkout bridge.
121+ */
32122 receivingBridgeEventError = 'error_receiving_message' ,
123+
124+ /**
125+ * The WebView render process has terminated unexpectedly (Android only).
126+ */
33127 renderProcessGone = 'render_process_gone' ,
128+
129+ // ============================================================================
130+ // Fallback
131+ // ============================================================================
132+
133+ /**
134+ * An unknown or unrecognized error code was received.
135+ */
34136 unknown = 'unknown' ,
35137}
36138
@@ -44,20 +146,27 @@ export enum CheckoutNativeErrorType {
44146}
45147
46148/**
47- * @important TODO: Fix mapping here.
48- * Currently "unknown" is returned for every code
149+ * Maps a native error code string to a CheckoutErrorCode enum value.
49150 */
50151function getCheckoutErrorCode ( code : string | undefined ) : CheckoutErrorCode {
152+ if ( ! code ) {
153+ return CheckoutErrorCode . unknown ;
154+ }
155+
156+ const normalizedCode = code . toLowerCase ( ) ;
157+
51158 const codeKey = Object . keys ( CheckoutErrorCode ) . find (
52- key => CheckoutErrorCode [ key as keyof typeof CheckoutErrorCode ] === code ,
159+ key => CheckoutErrorCode [ key as keyof typeof CheckoutErrorCode ] === normalizedCode ,
53160 ) ;
54161
55- return codeKey ? CheckoutErrorCode [ codeKey ] : CheckoutErrorCode . unknown ;
162+ return codeKey
163+ ? CheckoutErrorCode [ codeKey as keyof typeof CheckoutErrorCode ]
164+ : CheckoutErrorCode . unknown ;
56165}
57166
58167type BridgeError = {
59168 __typename : CheckoutNativeErrorType ;
60- code : CheckoutErrorCode ;
169+ code : string ;
61170 message : string ;
62171 recoverable : boolean ;
63172} ;
@@ -119,11 +228,13 @@ export class InternalError {
119228 code : CheckoutErrorCode ;
120229 message : string ;
121230 recoverable : boolean ;
231+ name : string ;
122232
123233 constructor ( exception : CheckoutNativeError ) {
124234 this . code = getCheckoutErrorCode ( exception . code ) ;
125235 this . message = exception . message ;
126236 this . recoverable = exception . recoverable ;
237+ this . name = this . constructor . name ;
127238 }
128239}
129240
@@ -134,3 +245,29 @@ export type CheckoutException =
134245 | ConfigurationError
135246 | GenericError
136247 | InternalError ;
248+
249+ /**
250+ * Transforms a native error object into the appropriate CheckoutException class.
251+ * Maps __typename to the correct error class and normalizes error codes.
252+ *
253+ * @param exception Raw error object from native bridge
254+ * @returns Appropriate CheckoutException instance
255+ */
256+ export function parseCheckoutError (
257+ exception : CheckoutNativeError ,
258+ ) : CheckoutException {
259+ switch ( exception ?. __typename ) {
260+ case CheckoutNativeErrorType . InternalError :
261+ return new InternalError ( exception ) ;
262+ case CheckoutNativeErrorType . ConfigurationError :
263+ return new ConfigurationError ( exception ) ;
264+ case CheckoutNativeErrorType . CheckoutClientError :
265+ return new CheckoutClientError ( exception ) ;
266+ case CheckoutNativeErrorType . CheckoutHTTPError :
267+ return new CheckoutHTTPError ( exception ) ;
268+ case CheckoutNativeErrorType . CheckoutExpiredError :
269+ return new CheckoutExpiredError ( exception ) ;
270+ default :
271+ return new GenericError ( exception ) ;
272+ }
273+ }
0 commit comments