| sidebar_position | 2 |
|---|
import GreatFrontEndTopFixed from "@site/src/uis/GreatFrontEndTopFixed";
React Native IAP provides a centralized error handling system with platform-specific error code mapping. This ensures consistent error handling across iOS and Android platforms.
The ErrorCode enum provides standardized error codes that map to platform-specific errors:
import {ErrorCode} from 'react-native-iap';
export enum ErrorCode {
Unknown = 'unknown',
UserCancelled = 'user-cancelled',
UserError = 'user-error',
ItemUnavailable = 'item-unavailable',
RemoteError = 'remote-error',
NetworkError = 'network-error',
ServiceError = 'service-error',
ReceiptFailed = 'receipt-failed',
ReceiptFinished = 'receipt-finished',
ReceiptFinishedFailed = 'receipt-finished-failed',
NotPrepared = 'not-prepared',
NotEnded = 'not-ended',
AlreadyOwned = 'already-owned',
DeveloperError = 'developer-error',
BillingResponseJsonParseError = 'billing-response-json-parse-error',
DeferredPayment = 'deferred-payment',
Interrupted = 'interrupted',
IapNotAvailable = 'iap-not-available',
PurchaseError = 'purchase-error',
SyncError = 'sync-error',
TransactionValidationFailed = 'transaction-validation-failed',
ActivityUnavailable = 'activity-unavailable',
AlreadyPrepared = 'already-prepared',
Pending = 'pending',
ConnectionClosed = 'connection-closed',
InitConnection = 'init-connection',
ServiceDisconnected = 'service-disconnected',
QueryProduct = 'query-product',
SkuNotFound = 'sku-not-found',
SkuOfferMismatch = 'sku-offer-mismatch',
ItemNotOwned = 'item-not-owned',
BillingUnavailable = 'billing-unavailable',
FeatureNotSupported = 'feature-not-supported',
EmptySkuList = 'empty-sku-list',
}A custom error class for purchase-related errors.
export interface PurchaseErrorProps {
message: string;
responseCode?: number;
debugMessage?: string;
code?: ErrorCode;
productId?: string;
platform?: IapPlatform;
}
export type PurchaseError = Error & PurchaseErrorProps;Creates a PurchaseError instance.
export const createPurchaseError = (
props: PurchaseErrorProps,
): PurchaseError => {
// ...
};Creates a PurchaseError from platform-specific error data.
export const createPurchaseErrorFromPlatform = (
errorData: PurchaseErrorProps,
platform: IapPlatform,
): PurchaseError => {
// ...
};Utility functions for error code mapping and validation.
Gets the native error code for the current platform:
ErrorCodeUtils.getNativeErrorCode(errorCode: ErrorCode): stringMaps platform-specific error code to standardized ErrorCode:
ErrorCodeUtils.fromPlatformCode(
platformCode: string | number,
platform?: IapPlatform,
): ErrorCodeMaps ErrorCode to platform-specific code:
ErrorCodeUtils.toPlatformCode(
errorCode: ErrorCode,
platform?: IapPlatform,
): string | numberChecks if error code is valid for the specified platform:
ErrorCodeUtils.isValidForPlatform(
errorCode: ErrorCode,
platform: IapPlatform,
): booleanThese functions help interpret error objects.
Returns true if the error is a user cancellation error.
export function isUserCancelledError(error: unknown): boolean;Returns true if the error is a network-related error.
export function isNetworkError(error: unknown): boolean;Returns true if the error is a recoverable error.
export function isRecoverableError(error: unknown): boolean;Returns a user-friendly error message for a given error.
export function getUserFriendlyErrorMessage(error: ErrorLike): string;The getUserFriendlyErrorMessage function provides localized and user-friendly messages for common errors.
| ErrorCode | Message |
|---|---|
UserCancelled |
'Purchase cancelled' |
NetworkError |
'Network connection error. Please check your internet connection and try again.' |
ReceiptFinished |
'Receipt already finished' |
ServiceDisconnected |
'Billing service disconnected. Please try again.' |
BillingUnavailable |
'Billing is unavailable on this device or account.' |
ItemUnavailable |
'This item is not available for purchase' |
ItemNotOwned |
"You don't own this item" |
AlreadyOwned |
'You already own this item' |
SkuNotFound |
'Requested product could not be found' |
SkuOfferMismatch |
'Selected offer does not match the SKU' |
DeferredPayment |
'Payment is pending approval' |
NotPrepared |
'In-app purchase is not ready. Please try again later.' |
ServiceError |
'Store service error. Please try again later.' |
FeatureNotSupported |
'This feature is not supported on this device.' |
TransactionValidationFailed |
'Transaction could not be verified' |
ReceiptFailed |
'Receipt processing failed' |
EmptySkuList |
'No product IDs provided' |
InitConnection |
'Failed to initialize billing connection' |
QueryProduct |
'Failed to query products. Please try again later.' |
| default | 'An unexpected error occurred' |