From e5ea09dd3a13ec47ca65de93c6f5858c1fadb135 Mon Sep 17 00:00:00 2001 From: c0mrade Date: Sun, 1 Feb 2026 20:29:38 +0300 Subject: [PATCH] fix: wrap all SDK isSupported() calls in try-catch --- src/hooks/useTelegramSDK.ts | 32 ++++++++++++++++++------ src/platform/adapters/TelegramAdapter.ts | 28 +++++++++++++-------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/hooks/useTelegramSDK.ts b/src/hooks/useTelegramSDK.ts index 81b46dd..9994e5a 100644 --- a/src/hooks/useTelegramSDK.ts +++ b/src/hooks/useTelegramSDK.ts @@ -88,9 +88,13 @@ export function initTelegramSDK() { } // Disable vertical swipes if supported (sync) - if (swipeBehavior.isSupported()) { - swipeBehavior.mount(); - swipeBehavior.disableVertical(); + try { + if (swipeBehavior.isSupported()) { + swipeBehavior.mount(); + swipeBehavior.disableVertical(); + } + } catch { + // Swipe behavior not available } // Mount viewport and bind CSS variables (async) @@ -233,20 +237,32 @@ export function useTelegramSDK() { const expand = useCallback(() => { if (!isReady) return; - viewport.expand(); + try { + viewport.expand(); + } catch { + // Viewport not available + } }, [isReady]); const disableVerticalSwipes = useCallback(() => { if (!isReady) return; - if (swipeBehavior.isSupported()) { - swipeBehavior.disableVertical(); + try { + if (swipeBehavior.isSupported()) { + swipeBehavior.disableVertical(); + } + } catch { + // Swipe behavior not available } }, [isReady]); const enableVerticalSwipes = useCallback(() => { if (!isReady) return; - if (swipeBehavior.isSupported()) { - swipeBehavior.enableVertical(); + try { + if (swipeBehavior.isSupported()) { + swipeBehavior.enableVertical(); + } + } catch { + // Swipe behavior not available } }, [isReady]); diff --git a/src/platform/adapters/TelegramAdapter.ts b/src/platform/adapters/TelegramAdapter.ts index bf50dd6..98843c3 100644 --- a/src/platform/adapters/TelegramAdapter.ts +++ b/src/platform/adapters/TelegramAdapter.ts @@ -29,18 +29,26 @@ function getTelegram(): TelegramWebApp | null { return window.Telegram?.WebApp ?? null; } +function safeIsSupported(checkFn: () => boolean): boolean { + try { + return checkFn(); + } catch { + return false; + } +} + function createCapabilities(): PlatformCapabilities { const tg = getTelegram(); const inTelegram = isInTelegramWebApp(); return { - hasBackButton: inTelegram && backButton.isSupported(), + hasBackButton: inTelegram && safeIsSupported(() => backButton.isSupported()), hasMainButton: inTelegram, - hasHapticFeedback: inTelegram && hapticFeedback.isSupported(), - hasNativeDialogs: inTelegram && popup.isSupported(), + hasHapticFeedback: inTelegram && safeIsSupported(() => hapticFeedback.isSupported()), + hasNativeDialogs: inTelegram && safeIsSupported(() => popup.isSupported()), hasThemeSync: inTelegram, hasInvoice: !!tg?.openInvoice, - hasCloudStorage: inTelegram && cloudStorage.isSupported(), + hasCloudStorage: inTelegram && safeIsSupported(() => cloudStorage.isSupported()), hasShare: true, version: tg?.version, }; @@ -53,7 +61,7 @@ function createBackButtonController(): BackButtonController { // Mount back button on first use let mounted = false; const ensureMounted = () => { - if (!mounted && inTelegram && backButton.isSupported()) { + if (!mounted && inTelegram && safeIsSupported(() => backButton.isSupported())) { try { backButton.mount(); mounted = true; @@ -74,7 +82,7 @@ function createBackButtonController(): BackButtonController { }, show(onClick: () => void) { - if (!inTelegram || !backButton.isSupported()) return; + if (!inTelegram || !safeIsSupported(() => backButton.isSupported())) return; ensureMounted(); @@ -89,7 +97,7 @@ function createBackButtonController(): BackButtonController { }, hide() { - if (!inTelegram || !backButton.isSupported()) return; + if (!inTelegram || !safeIsSupported(() => backButton.isSupported())) return; if (removeClickListener) { removeClickListener(); @@ -193,7 +201,7 @@ function createMainButtonController(): MainButtonController { function createHapticController(): HapticController { const inTelegram = isInTelegramWebApp(); - const isSupported = inTelegram && hapticFeedback.isSupported(); + const isSupported = inTelegram && safeIsSupported(() => hapticFeedback.isSupported()); return { impact(style: HapticImpactStyle = 'medium') { @@ -215,7 +223,7 @@ function createHapticController(): HapticController { function createDialogController(): DialogController { const inTelegram = isInTelegramWebApp(); - const isSupported = inTelegram && popup.isSupported(); + const isSupported = inTelegram && safeIsSupported(() => popup.isSupported()); return { alert(message: string, _title?: string): Promise { @@ -337,7 +345,7 @@ function createThemeController(): ThemeController { function createCloudStorageController(): CloudStorageController | null { const inTelegram = isInTelegramWebApp(); - if (!inTelegram || !cloudStorage.isSupported()) return null; + if (!inTelegram || !safeIsSupported(() => cloudStorage.isSupported())) return null; return { async getItem(key: string): Promise {