From 86403ae6491b889bf9cba686eb9c9b498ef68097 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sat, 18 Jul 2026 13:51:39 +0200 Subject: [PATCH] fix(notifications): handle missing UnifiedPush distributor command on Android --- .../settings/notifications/SystemNotification.tsx | 9 +++++---- .../notifications/UnifiedPushTransport.test.ts | 6 ++++-- .../settings/notifications/UnifiedPushTransport.ts | 13 ++++++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/app/features/settings/notifications/SystemNotification.tsx b/src/app/features/settings/notifications/SystemNotification.tsx index f84a6b227..4a5c84a1d 100644 --- a/src/app/features/settings/notifications/SystemNotification.tsx +++ b/src/app/features/settings/notifications/SystemNotification.tsx @@ -62,6 +62,7 @@ import { tryEnableUnifiedPush, type UnifiedPushTransportConfigInput, } from './UnifiedPushNotifications'; +import { normalizeErrorMessage } from './UnifiedPushTransport'; import { ensureUnifiedPushDistributorSelection, loadUnifiedPushDistributorState, @@ -564,7 +565,7 @@ function BackgroundPushNotificationSetting() { }) .catch((caughtError) => { if (!active) return; - setError(caughtError instanceof Error ? caughtError.message : String(caughtError)); + setError(normalizeErrorMessage(caughtError)); }) .finally(() => { if (active) setIsLoading(false); @@ -770,7 +771,7 @@ function BackgroundPushNotificationSetting() { } setBackgroundPushEnabled(wantsPush); } catch (caughtError) { - setError(caughtError instanceof Error ? caughtError.message : String(caughtError)); + setError(normalizeErrorMessage(caughtError)); } finally { setIsLoading(false); } @@ -795,7 +796,7 @@ function BackgroundPushNotificationSetting() { } setPushTransportMode(nextMode); } catch (caughtError) { - setError(caughtError instanceof Error ? caughtError.message : String(caughtError)); + setError(normalizeErrorMessage(caughtError)); } finally { setIsLoading(false); } @@ -827,7 +828,7 @@ function BackgroundPushNotificationSetting() { setSelectedDistributor(distributor); updatePushTransportOverride({ unifiedPushDistributor: distributor }); } catch (caughtError) { - setError(caughtError instanceof Error ? caughtError.message : String(caughtError)); + setError(normalizeErrorMessage(caughtError)); } finally { setIsLoading(false); } diff --git a/src/app/features/settings/notifications/UnifiedPushTransport.test.ts b/src/app/features/settings/notifications/UnifiedPushTransport.test.ts index b94529df1..872efe7a4 100644 --- a/src/app/features/settings/notifications/UnifiedPushTransport.test.ts +++ b/src/app/features/settings/notifications/UnifiedPushTransport.test.ts @@ -186,11 +186,13 @@ describe('UnifiedPush distributor state helpers', () => { ); }); - it('surfaces backend errors while loading distributor state', async () => { + it('returns empty distributors when the backend is unavailable', async () => { localStorage.removeItem('unifiedpush_distributor'); unifiedPushApi.listDistributors.mockRejectedValue(new Error('backend unavailable')); - await expect(loadUnifiedPushDistributorState()).rejects.toThrow('backend unavailable'); + const result = await loadUnifiedPushDistributorState(); + expect(result.distributors).toEqual([]); + expect(result.selectedDistributor).toBe(''); }); it('restores the previous distributor when a switch registration fails', async () => { diff --git a/src/app/features/settings/notifications/UnifiedPushTransport.ts b/src/app/features/settings/notifications/UnifiedPushTransport.ts index e0b29a1cf..a07261ac3 100644 --- a/src/app/features/settings/notifications/UnifiedPushTransport.ts +++ b/src/app/features/settings/notifications/UnifiedPushTransport.ts @@ -48,7 +48,7 @@ export type UnifiedPushRegistrationResult = const DISTRIBUTUTOR_STORAGE_KEY = 'unifiedpush_distributor'; -function normalizeErrorMessage(error: unknown): string { +export function normalizeErrorMessage(error: unknown): string { if (error instanceof Error) return error.message; if (typeof error === 'string') return error; if (error && typeof error === 'object') { @@ -125,11 +125,18 @@ export async function saveUnifiedPushDistributor(distributor: string): Promise { - const [distributorResult, distributors] = await Promise.all([ + const [distributorResult, distributorsResult] = await Promise.allSettled([ getUnifiedPushDistributor(), getUnifiedPushDistributors(), ]); - const savedDistributor = distributorResult.distributor; + + const savedDistributor = + distributorsResult.status === 'fulfilled' + ? distributorResult.status === 'fulfilled' + ? distributorResult.value.distributor + : '' + : ''; + const distributors = distributorsResult.status === 'fulfilled' ? distributorsResult.value : []; if (savedDistributor && distributors.includes(savedDistributor)) { return { distributors, selectedDistributor: savedDistributor };