在 Kotlin Multiplatform SDK 中处理错误
安装 AI Agent 工具
在你的 AI 编程工具中完成整个 Adapty SDK 集成。 了解更多
claude plugin marketplace add adaptyteam/adapty-sdk-integration-skill
claude plugin install adapty-sdk-integration@adapty
git clone https://github.com/adaptyteam/adapty-sdk-integration-skill.git
cp -r adapty-sdk-integration-skill/skills/* ~/.copilot/skills/
gemini skills install https://github.com/adaptyteam/adapty-sdk-integration-skill
git clone https://github.com/adaptyteam/adapty-sdk-integration-skill.git
cp -r adapty-sdk-integration-skill/skills/* ~/.agents/skills/
可安装到任何支持技能的 AI 工具。之后更新请运行 npx skills update。
npx skills add adaptyteam/adapty-sdk-integration-skill --all
安装后,在你的项目中运行该技能:
/adapty-sdk-integration
本页介绍 Adapty Kotlin Multiplatform SDK 中的错误处理方式。
错误处理基础
Adapty SDK 的所有方法都会返回成功或错误结果。请务必处理这两种情况:
Adapty.getProfile { result ->
when (result) {
is AdaptyResult.Success -> {
val profile = result.value
// Handle success
}
is AdaptyResult.Error -> {
val error = result.error
// Handle error
Log.e("Adapty", "Error: ${error.message}")
}
}
} Adapty.getProfile(result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
// Handle success
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
// Handle error
Log.e("Adapty", "Error: " + error.getMessage());
}
}); 常见错误代码
AdaptyError.code 是一个 AdaptyErrorCode 枚举值,而非数字——请与枚举常量进行比较,不要与数字代码进行比较。下方列出的数字仅供参考,通常出现在日志记录或支持工单中。
| 错误代码 | 编号 | 描述 | 解决方案 |
|---|---|---|---|
AdaptyErrorCode.NO_PRODUCT_IDS_FOUND | 1000 | 付费墙中的产品在商店中均不可用。 | 请参阅 Code-1000 noProductIDsFound 错误修复方案。 |
AdaptyErrorCode.CANT_MAKE_PAYMENTS | 1003 | 此设备不允许应用内购买。 | 请参阅 Code-1003 cantMakePayments 错误修复方案。 |
AdaptyErrorCode.PRODUCT_NOT_FOUND | 22 | 请求购买的产品在商店中不可用。 | 请检查该产品是否已在商店和 Adapty 看板中正确配置。 |
AdaptyErrorCode.NETWORK_FAILED | 2005 | 网络请求失败。 | 提示用户检查网络连接,或重试该调用。 |
AdaptyErrorCode.ADAPTY_NOT_INITIALIZED | 20 | 调用前未激活 SDK。 | 请等待 Adapty.activate 完成后再进行其他 SDK 调用。 |
完整的错误码列表,请参阅 SDK 模型参考 中的 AdaptyErrorCode。
处理特定错误
网络错误
Adapty.getPaywall("main") { result ->
when (result) {
is AdaptyResult.Success -> {
val paywall = result.value
// Use paywall
}
is AdaptyResult.Error -> {
val error = result.error
when (error.code) {
AdaptyErrorCode.NETWORK_FAILED -> {
// Network error - show offline message
showOfflineMessage()
}
else -> {
// Other errors
showErrorMessage(error.message)
}
}
}
}
} Adapty.getPaywall("main", result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyPaywall paywall = ((AdaptyResult.Success<AdaptyPaywall>) result).getValue();
// Use paywall
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
switch (error.getCode()) {
case NETWORK_FAILED:
// Network error - show offline message
showOfflineMessage();
break;
default:
// Other errors
showErrorMessage(error.getMessage());
break;
}
}
}); 购买错误
product.makePurchase { result ->
when (result) {
is AdaptyResult.Success -> {
val purchase = result.value
// Purchase successful
showSuccessMessage()
}
is AdaptyResult.Error -> {
val error = result.error
when (error.code) {
AdaptyErrorCode.CANT_MAKE_PAYMENTS -> {
// Can't make payments
showPaymentNotAvailableMessage()
}
AdaptyErrorCode.PRODUCT_NOT_FOUND -> {
// Product not available
showProductNotAvailableMessage()
}
else -> {
// Other purchase errors
showPurchaseErrorMessage(error.message)
}
}
}
}
} product.makePurchase(result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyPurchase purchase = ((AdaptyResult.Success<AdaptyPurchase>) result).getValue();
// Purchase successful
showSuccessMessage();
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
switch (error.getCode()) {
case CANT_MAKE_PAYMENTS:
// Can't make payments
showPaymentNotAvailableMessage();
break;
case PRODUCT_NOT_FOUND:
// Product not available
showProductNotAvailableMessage();
break;
default:
// Other purchase errors
showPurchaseErrorMessage(error.getMessage());
break;
}
}
}); 错误恢复策略
网络错误重试
fun getPaywallWithRetry(placementId: String, maxRetries: Int = 3) {
var retryCount = 0
fun attemptGetPaywall() {
Adapty.getPaywall(placementId) { result ->
when (result) {
is AdaptyResult.Success -> {
val paywall = result.value
// Use paywall
}
is AdaptyResult.Error -> {
val error = result.error
if (error.code == AdaptyErrorCode.NETWORK_FAILED && retryCount < maxRetries) {
// Network error - retry
retryCount++
Handler(Looper.getMainLooper()).postDelayed({
attemptGetPaywall()
}, 1000 * retryCount) // Exponential backoff
} else {
// Max retries reached or other error
showErrorMessage(error.message)
}
}
}
}
}
attemptGetPaywall()
} public void getPaywallWithRetry(String placementId, int maxRetries) {
AtomicInteger retryCount = new AtomicInteger(0);
Runnable attemptGetPaywall = new Runnable() {
@Override
public void run() {
Adapty.getPaywall(placementId, result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyPaywall paywall = ((AdaptyResult.Success<AdaptyPaywall>) result).getValue();
// Use paywall
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
if (error.getCode() == AdaptyErrorCode.NETWORK_FAILED && retryCount.get() < maxRetries) {
// Network error - retry
retryCount.incrementAndGet();
new Handler(Looper.getMainLooper()).postDelayed(this, 1000 * retryCount.get());
} else {
// Max retries reached or other error
showErrorMessage(error.getMessage());
}
}
});
}
};
attemptGetPaywall.run();
} 备用缓存数据
class PaywallManager {
private var cachedPaywall: AdaptyPaywall? = null
fun getPaywall(placementId: String) {
Adapty.getPaywall(placementId) { result ->
when (result) {
is AdaptyResult.Success -> {
val paywall = result.value
cachedPaywall = paywall
showPaywall(paywall)
}
is AdaptyResult.Error -> {
val error = result.error
if (error.code == AdaptyErrorCode.NETWORK_FAILED && cachedPaywall != null) {
// Network error - use cached paywall
showPaywall(cachedPaywall!!)
showOfflineIndicator()
} else {
// No cache available or other error
showErrorMessage(error.message)
}
}
}
}
}
} public class PaywallManager {
private AdaptyPaywall cachedPaywall;
public void getPaywall(String placementId) {
Adapty.getPaywall(placementId, result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyPaywall paywall = ((AdaptyResult.Success<AdaptyPaywall>) result).getValue();
cachedPaywall = paywall;
showPaywall(paywall);
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
if (error.getCode() == AdaptyErrorCode.NETWORK_FAILED && cachedPaywall != null) {
// 网络错误 - 使用缓存的付费墙
showPaywall(cachedPaywall);
showOfflineIndicator();
} else {
// 无缓存或其他错误
showErrorMessage(error.getMessage());
}
}
});
}
}