Files
keyboard/keyBoard/Class/Pay/StoreKit2Manager/Models/StoreKitError.swift
2025-12-16 13:10:50 +08:00

86 lines
2.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// StoreKitError.swift
// StoreKit2Manager
//
// Created by xiaopin on 2025/12/6.
//
import Foundation
/// StoreKit
public enum StoreKitError: Error, LocalizedError {
///
case productNotFound(String)
///
case purchaseFailed(Error)
///
case verificationFailed
///
case configurationMissing
///
case serviceNotStarted
///
case purchaseInProgress
///
case cancelSubscriptionFailed(Error)
///
case restorePurchasesFailed(Error)
///
case unknownError
public var errorDescription: String? {
switch self {
case .productNotFound(let id):
return "产品未找到: \(id)"
case .purchaseFailed(let error):
return "购买失败: \(error.localizedDescription)"
case .verificationFailed:
return "交易验证失败,可能是设备已越狱或交易数据被篡改"
case .configurationMissing:
return "配置缺失,请先调用 configure 方法进行配置"
case .serviceNotStarted:
return "服务未启动,请先调用 configure 方法"
case .purchaseInProgress:
return "购买正在进行中,请等待当前购买完成"
case .cancelSubscriptionFailed(let error):
return "取消订阅失败: \(error.localizedDescription)"
case .restorePurchasesFailed(let error):
return "恢复购买失败: \(error.localizedDescription)"
case .unknownError:
return "未知错误"
}
}
public var failureReason: String? {
switch self {
case .productNotFound(let id):
return "请检查产品ID是否正确并确保在 App Store Connect 中已配置该产品"
case .purchaseFailed(let error):
return error.localizedDescription
case .verificationFailed:
return "交易数据无法通过 Apple 的验证,这可能是由于设备已越狱或交易数据被篡改"
case .configurationMissing:
return "在调用其他方法之前,必须先调用 configure(with:delegate:) 或 configure(with:onStateChanged:) 方法"
case .serviceNotStarted:
return "StoreKit2Manager 尚未初始化,请先调用 configure 方法"
case .purchaseInProgress:
return "当前有购买正在进行,请等待完成后再试"
case .cancelSubscriptionFailed(let error):
return (error as? LocalizedError)?.failureReason ?? error.localizedDescription
case .restorePurchasesFailed(let error):
return (error as? LocalizedError)?.failureReason ?? error.localizedDescription
case .unknownError:
return "发生了未预期的错误"
}
}
}