getUserFriendlyErrorMessage function
Returns a user-friendly message for the given error or error code
Implementation
String getUserFriendlyErrorMessage(dynamic error) {
final ErrorCode code = _normalizeToErrorCode(error);
switch (code) {
case ErrorCode.UserCancelled:
return 'Purchase was cancelled by user';
case ErrorCode.NetworkError:
return 'Network connection error. Please check your internet connection and try again.';
case ErrorCode.ItemUnavailable:
case ErrorCode.SkuNotFound:
return 'This item is not available for purchase';
case ErrorCode.AlreadyOwned:
return 'You already own this item';
case ErrorCode.DeferredPayment:
return 'Payment is pending approval';
case ErrorCode.NotPrepared:
return 'In-app purchase is not ready. Please try again later.';
case ErrorCode.ServiceError:
return 'Store service error. Please try again later.';
case ErrorCode.TransactionValidationFailed:
return 'Transaction could not be verified';
case ErrorCode.ReceiptFailed:
return 'Receipt processing failed';
default:
// Try to surface message from PurchaseError if available
if (error is PurchaseError && error.message.isNotEmpty) {
return error.message;
}
if (error is Map && error['message'] is String) {
return error['message'] as String;
}
return 'An unexpected error occurred';
}
}