Skip to content

Commit 7949a71

Browse files
committed
store: Handle invalid API key on register-queue
Fixes: zulip#737 Signed-off-by: Zixuan James Li <[email protected]>
1 parent 930ef5b commit 7949a71

15 files changed

+343
-7
lines changed

assets/l10n/app_en.arb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@
458458
"@topicValidationErrorMandatoryButEmpty": {
459459
"description": "Topic validation error when topic is required but was empty."
460460
},
461+
"errorInvalidApiKeyMessage": "Your account at {url} could not be authenticated. Please try logging in again or use another account.",
462+
"@errorInvalidApiKeyMessage": {
463+
"description": "Error message in the dialog for invalid API key.",
464+
"placeholders": {
465+
"url": {"type": "String", "example": "http://chat.example.com/"}
466+
}
467+
},
461468
"errorInvalidResponse": "The server sent an invalid response",
462469
"@errorInvalidResponse": {
463470
"description": "Error message when an API call returned an invalid response."

lib/generated/l10n/zulip_localizations.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ abstract class ZulipLocalizations {
723723
/// **'Topics are required in this organization.'**
724724
String get topicValidationErrorMandatoryButEmpty;
725725

726+
/// Error message in the dialog for invalid API key.
727+
///
728+
/// In en, this message translates to:
729+
/// **'Your account at {url} could not be authenticated. Please try logging in again or use another account.'**
730+
String errorInvalidApiKeyMessage(String url);
731+
726732
/// Error message when an API call returned an invalid response.
727733
///
728734
/// In en, this message translates to:

lib/generated/l10n/zulip_localizations_ar.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsAr extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_en.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsEn extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_ja.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsJa extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_nb.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsNb extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_pl.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsPl extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Wątki są wymagane przez tę organizację.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'Nieprawidłowa odpowiedź serwera';
362367

lib/generated/l10n/zulip_localizations_ru.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsRu extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Темы обязательны в этой организации.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'Получен недопустимый ответ сервера';
362367

lib/generated/l10n/zulip_localizations_sk.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsSk extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'Server poslal nesprávnu odpoveď';
362367

lib/model/store.dart

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import '../api/backoff.dart';
1919
import '../api/route/realm.dart';
2020
import '../log.dart';
2121
import '../notifications/receive.dart';
22+
import 'actions.dart';
2223
import 'autocomplete.dart';
2324
import 'database.dart';
2425
import 'emoji.dart';
@@ -149,13 +150,31 @@ abstract class GlobalStore extends ChangeNotifier {
149150
/// and/or [perAccountSync].
150151
Future<PerAccountStore> loadPerAccount(int accountId) async {
151152
assert(_accounts.containsKey(accountId));
152-
final store = await doLoadPerAccount(accountId);
153+
PerAccountStore? store;
154+
try {
155+
store = await doLoadPerAccount(accountId);
156+
} catch (e) {
157+
switch (e) {
158+
case ZulipApiException(code: 'INVALID_API_KEY'):
159+
// The API key is invalid and the store can never be loaded
160+
// unless the user retries manually.
161+
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
162+
final account = getAccount(accountId)!;
163+
reportErrorToUserModally(
164+
zulipLocalizations.errorCouldNotConnectTitle,
165+
details: zulipLocalizations.errorInvalidApiKeyMessage(
166+
account.realmUrl.toString()));
167+
await logOutAccount(this, accountId);
168+
default:
169+
rethrow;
170+
}
171+
}
153172
if (!_accounts.containsKey(accountId)) {
154-
// [removeAccount] was called during [doLoadPerAccount].
155-
store.dispose();
173+
// [removeAccount] was called during or after [doLoadPerAccount].
174+
store?.dispose();
156175
throw AccountNotFoundException();
157176
}
158-
return store;
177+
return store!;
159178
}
160179

161180
/// Load per-account data for the given account, unconditionally.
@@ -912,7 +931,13 @@ class UpdateMachine {
912931
// at 1 kiB (at least on Android), and stack can be longer than that.
913932
assert(debugLog('Stack:\n$s'));
914933
assert(debugLog('Backing off, then will retry…'));
915-
// TODO tell user if initial-fetch errors persist, or look non-transient
934+
// TODO(#890): tell user if initial-fetch errors persist, or look non-transient
935+
switch (e) {
936+
case ZulipApiException(code: 'INVALID_API_KEY'):
937+
// We cannot recover from this error through retrying.
938+
// Leave it to [GlobalStore.loadPerAccount].
939+
rethrow;
940+
}
916941
await (backoffMachine ??= BackoffMachine()).wait();
917942
assert(debugLog('… Backoff wait complete, retrying initial fetch.'));
918943
}
@@ -1044,7 +1069,13 @@ class UpdateMachine {
10441069
}
10451070
} catch (e) {
10461071
if (_disposed) return;
1047-
await _handlePollError(e);
1072+
try {
1073+
await _handlePollError(e);
1074+
} on AccountNotFoundException {
1075+
// Cannot recover by replacing the store because the account
1076+
// was logged out.
1077+
return;
1078+
}
10481079
assert(_disposed);
10491080
return;
10501081
}

0 commit comments

Comments
 (0)