expo-iap 2.4.4 → 2.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Error mapping utilities for expo-iap
3
+ * Provides helper functions for handling platform-specific errors
4
+ */
5
+
6
+ import {ErrorCode} from '../ExpoIap.types';
7
+
8
+ /**
9
+ * Checks if an error is a user cancellation
10
+ * @param error Error object or error code
11
+ * @returns True if the error represents user cancellation
12
+ */
13
+ export function isUserCancelledError(error: any): boolean {
14
+ if (typeof error === 'string') {
15
+ return error === ErrorCode.E_USER_CANCELLED;
16
+ }
17
+
18
+ if (error && error.code) {
19
+ return error.code === ErrorCode.E_USER_CANCELLED;
20
+ }
21
+
22
+ return false;
23
+ }
24
+
25
+ /**
26
+ * Checks if an error is related to network connectivity
27
+ * @param error Error object or error code
28
+ * @returns True if the error is network-related
29
+ */
30
+ export function isNetworkError(error: any): boolean {
31
+ const networkErrors = [
32
+ ErrorCode.E_NETWORK_ERROR,
33
+ ErrorCode.E_REMOTE_ERROR,
34
+ ErrorCode.E_SERVICE_ERROR,
35
+ ];
36
+
37
+ const errorCode = typeof error === 'string' ? error : error?.code;
38
+ return networkErrors.includes(errorCode);
39
+ }
40
+
41
+ /**
42
+ * Checks if an error is recoverable (user can retry)
43
+ * @param error Error object or error code
44
+ * @returns True if the error is potentially recoverable
45
+ */
46
+ export function isRecoverableError(error: any): boolean {
47
+ const recoverableErrors = [
48
+ ErrorCode.E_NETWORK_ERROR,
49
+ ErrorCode.E_REMOTE_ERROR,
50
+ ErrorCode.E_SERVICE_ERROR,
51
+ ErrorCode.E_INTERRUPTED,
52
+ ];
53
+
54
+ const errorCode = typeof error === 'string' ? error : error?.code;
55
+ return recoverableErrors.includes(errorCode);
56
+ }
57
+
58
+ /**
59
+ * Gets a user-friendly error message for display
60
+ * @param error Error object or error code
61
+ * @returns User-friendly error message
62
+ */
63
+ export function getUserFriendlyErrorMessage(error: any): string {
64
+ const errorCode = typeof error === 'string' ? error : error?.code;
65
+
66
+ switch (errorCode) {
67
+ case ErrorCode.E_USER_CANCELLED:
68
+ return 'Purchase was cancelled by user';
69
+ case ErrorCode.E_NETWORK_ERROR:
70
+ return 'Network connection error. Please check your internet connection and try again.';
71
+ case ErrorCode.E_ITEM_UNAVAILABLE:
72
+ return 'This item is not available for purchase';
73
+ case ErrorCode.E_ALREADY_OWNED:
74
+ return 'You already own this item';
75
+ case ErrorCode.E_DEFERRED_PAYMENT:
76
+ return 'Payment is pending approval';
77
+ case ErrorCode.E_NOT_PREPARED:
78
+ return 'In-app purchase is not ready. Please try again later.';
79
+ case ErrorCode.E_SERVICE_ERROR:
80
+ return 'Store service error. Please try again later.';
81
+ case ErrorCode.E_TRANSACTION_VALIDATION_FAILED:
82
+ return 'Transaction could not be verified';
83
+ case ErrorCode.E_RECEIPT_FAILED:
84
+ return 'Receipt processing failed';
85
+ default:
86
+ return error?.message || 'An unexpected error occurred';
87
+ }
88
+ }