UNPKG

react-native-iap

Version:

React Native In-App Purchases module for iOS and Android using Nitro

57 lines (56 loc) 2.08 kB
"use strict"; import { ErrorCode } from "../types.js"; const ERROR_CODE_ALIASES = { E_USER_CANCELED: ErrorCode.UserCancelled, USER_CANCELED: ErrorCode.UserCancelled, E_USER_CANCELLED: ErrorCode.UserCancelled, USER_CANCELLED: ErrorCode.UserCancelled }; export const normalizeErrorCodeFromNative = code => { if (typeof code === 'string') { const upper = code.toUpperCase(); const alias = ERROR_CODE_ALIASES[upper]; if (alias) { return alias; } const trimmed = upper.startsWith('E_') ? upper.slice(2) : upper; const camel = trimmed.toLowerCase().split('_').map(segment => { if (!segment) return segment; return segment.charAt(0).toUpperCase() + segment.slice(1); }).join(''); if (ErrorCode[camel]) { return ErrorCode[camel]; } } return ErrorCode.Unknown; }; export function isUserCancelledError(error) { return normalizeErrorCodeFromNative(error.code) === ErrorCode.UserCancelled; } export function isRecoverableError(error) { const recoverable = new Set([ErrorCode.NetworkError, ErrorCode.ServiceError, ErrorCode.RemoteError, ErrorCode.ConnectionClosed, ErrorCode.ServiceDisconnected, ErrorCode.InitConnection, ErrorCode.SyncError]); return recoverable.has(error.code); } export function getUserFriendlyErrorMessage(error) { switch (error.code) { case ErrorCode.UserCancelled: return 'Purchase cancelled'; case ErrorCode.NetworkError: return 'Network connection error'; case ErrorCode.ServiceError: return 'Store service error'; case ErrorCode.RemoteError: return 'Remote service error'; case ErrorCode.IapNotAvailable: return 'In-app purchases are not available on this device'; case ErrorCode.DeferredPayment: return 'Payment was deferred (pending approval)'; case ErrorCode.TransactionValidationFailed: return 'Transaction validation failed'; case ErrorCode.SkuNotFound: return 'Product not found'; default: return error.message || 'Unknown error occurred'; } } //# sourceMappingURL=errorMapping.js.map