From 7324b2b2bfcdfbf72cb4d34cddad334953383c79 Mon Sep 17 00:00:00 2001 From: loveheaven Date: Tue, 26 May 2026 00:28:26 +0800 Subject: [PATCH] fix(ios): don't crash app init when Storefront region code is unavailable (#4291) `getStorefrontRegionCode` rejects when `Storefront.current` is nil (unsigned simulator, signed-out device, StoreKit not yet ready, parental controls, etc.). The call sits in NativeAppService startup before `prepareBooksDir`, so an unhandled rejection here aborts the rest of init and surfaces as a top-level unhandledRejection in the webview console. Wrap the call in try/catch and treat failure as 'unknown region', leaving `storefrontRegionCode` null so downstream region-gated features degrade gracefully. Also guard against an empty resolve shape with optional chaining on `res?.regionCode`. --- apps/readest-app/src/services/nativeAppService.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/readest-app/src/services/nativeAppService.ts b/apps/readest-app/src/services/nativeAppService.ts index a345bca0..067737c7 100644 --- a/apps/readest-app/src/services/nativeAppService.ts +++ b/apps/readest-app/src/services/nativeAppService.ts @@ -492,9 +492,18 @@ export class NativeAppService extends BaseAppService { } if (this.isIOSApp) { this.isOnlineCatalogsAccessible = this.distChannel !== 'appstore'; - const res = await getStorefrontRegionCode(); - if (res.regionCode) { - this.storefrontRegionCode = res.regionCode; + try { + const res = await getStorefrontRegionCode(); + if (res?.regionCode) { + this.storefrontRegionCode = res.regionCode; + } + } catch (err) { + // Storefront.current is nil on simulators without a signed-in + // App Store account, and may also fail on real devices with no + // StoreKit configuration. Treat as "unknown region" — we leave + // storefrontRegionCode as null and let downstream features that + // depend on region degrade gracefully. + console.warn('[nativeAppService] getStorefrontRegionCode failed:', err); } } await this.prepareBooksDir();