Surface a missing sync endpoint as its own error category

This commit is contained in:
Roland Geider
2026-08-21 16:04:21 +02:00
parent a0fd3ad0e2
commit a9f2c07914
4 changed files with 28 additions and 8 deletions

View File

@@ -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) {

View File

@@ -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';
}

View File

@@ -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');
});

View File

@@ -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<NoPowerSyncEndpointException>()));
await expectLater(connector.fetchCredentials(), throwsA(isA<NoPowerSyncEndpointException>()));
// Both candidates probed on both attempts: no negative caching.
expect(probes, hasLength(4));