From a9f2c079149cfdc36015c0b156cada1de376df9d Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 21 Aug 2026 16:04:21 +0200 Subject: [PATCH] Surface a missing sync endpoint as its own error category --- lib/powersync/connector.dart | 16 ++++++++++++---- lib/powersync/sync_diagnostics.dart | 7 ++++++- test/core/widgets/sync_status_dialog_test.dart | 9 ++++++++- test/powersync/connector_test.dart | 4 ++-- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/powersync/connector.dart b/lib/powersync/connector.dart index e7d480a5f..07654a938 100644 --- a/lib/powersync/connector.dart +++ b/lib/powersync/connector.dart @@ -52,6 +52,14 @@ class RetryableUploadException implements Exception { String toString() => 'Upload of $op on $table deferred: retryable status $statusCode'; } +/// Thrown when the wger backend answers but no live PowerSync endpoint can +/// be resolved: a down or misconfigured sync service, not an offline device +/// and not an authentication problem. +class NoPowerSyncEndpointException implements Exception { + @override + String toString() => 'No live PowerSync endpoint found'; +} + /// Stand-in for an unexpected upload error, carrying nothing but text. /// /// An error object that cannot be sent between isolates never reaches the @@ -163,11 +171,11 @@ class DjangoConnector extends PowerSyncBackendConnector { } } if (endpoint == null) { - // The wger backend answered (the token fetch above succeeded), so this - // is a bad or down sync service, not the device being offline. - // Returning null skips the attempt; PowerSync retries on its own. + // The wger backend answered (the token fetch above succeeded). Null + // would mean "not logged in" to the SDK; the typed error keeps the + // meaning. PowerSync retries on its own schedule either way. _logNoEndpoint(); - return null; + throw NoPowerSyncEndpointException(); } _lastNoEndpointLogAt = null; if (endpoint != _lastLoggedEndpoint) { diff --git a/lib/powersync/sync_diagnostics.dart b/lib/powersync/sync_diagnostics.dart index 612f9e83e..d558aa019 100644 --- a/lib/powersync/sync_diagnostics.dart +++ b/lib/powersync/sync_diagnostics.dart @@ -22,7 +22,8 @@ import 'package:powersync/powersync.dart' show CredentialsException, PowerSyncProtocolException, SyncResponseException, SyncStatus; import 'package:wger/core/consts.dart'; import 'package:wger/database/powersync/powersync.dart' show builtPowerSyncInstance; -import 'package:wger/powersync/connector.dart' show RetryableUploadException; +import 'package:wger/powersync/connector.dart' + show NoPowerSyncEndpointException, RetryableUploadException; final _logger = Logger('sync_diagnostics'); @@ -39,6 +40,10 @@ String _categoriseHttpStatus(int statusCode) { /// Classifies a sync error into a short English category label String? categoriseSyncError(Object error) { + if (error is NoPowerSyncEndpointException) { + return 'Sync service unavailable'; + } + if (error is CredentialsException) { return 'Authentication error'; } diff --git a/test/core/widgets/sync_status_dialog_test.dart b/test/core/widgets/sync_status_dialog_test.dart index 6efce4c68..db8c83510 100644 --- a/test/core/widgets/sync_status_dialog_test.dart +++ b/test/core/widgets/sync_status_dialog_test.dart @@ -36,7 +36,8 @@ import 'package:wger/database/powersync/powersync.dart' show pendingUploadCountProvider, syncStatus, syncWatchdogProvider; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/l10n/generated/app_localizations_en.dart'; -import 'package:wger/powersync/connector.dart' show RetryableUploadException; +import 'package:wger/powersync/connector.dart' + show NoPowerSyncEndpointException, RetryableUploadException; import 'package:wger/powersync/sync_watchdog.dart'; import '../../helpers/sync_status.dart'; @@ -393,6 +394,12 @@ void main() { await expectCategory(tester, CredentialsException('Not logged in'), 'Authentication error'); }); + testWidgets('NoPowerSyncEndpointException → "Sync service unavailable"', (tester) async { + // A dead or misconfigured sync service behind a working backend must + // not read as an authentication problem. + await expectCategory(tester, NoPowerSyncEndpointException(), 'Sync service unavailable'); + }); + testWidgets('SyncResponseException(401) → "Authentication error"', (tester) async { await expectCategory(tester, SyncResponseException(401, 'no token'), 'Authentication error'); }); diff --git a/test/powersync/connector_test.dart b/test/powersync/connector_test.dart index dddf3b3d5..8a8aa2ec9 100644 --- a/test/powersync/connector_test.dart +++ b/test/powersync/connector_test.dart @@ -352,8 +352,8 @@ void main() { (_) async => {'token': jwt, 'powersync_url': 'https://ps.example.com'}, ); - expect(await connector.fetchCredentials(), isNull); - expect(await connector.fetchCredentials(), isNull); + await expectLater(connector.fetchCredentials(), throwsA(isA())); + await expectLater(connector.fetchCredentials(), throwsA(isA())); // Both candidates probed on both attempts: no negative caching. expect(probes, hasLength(4));