Skip to content

Commit a43e320

Browse files
committed
store: Handle invalid API key on register-queue
This is near term fix for a user-reported issue: https://chat.zulip.org/#narrow/channel/48-mobile/topic/0.2E0.2E19.20Flutter.20.3A.20Cant.20connect.20to.20self.20hosted.20instance/near/2004042 It is not intended to be the full fix. With a better UX, we would bring the user back to the choose-account page without them manually doing so. That's covered by #737 but out-of-scope for this commit. Signed-off-by: Zixuan James Li <[email protected]>
1 parent eb3e2aa commit a43e320

15 files changed

+158
-18
lines changed

assets/l10n/app_en.arb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@
186186
"url": {"type": "String", "example": "http://example.com/"}
187187
}
188188
},
189-
"errorLoginCouldNotConnectTitle": "Could not connect",
190-
"@errorLoginCouldNotConnectTitle": {
189+
"errorCouldNotConnectTitle": "Could not connect",
190+
"@errorCouldNotConnectTitle": {
191191
"description": "Error title when the app could not connect to the server."
192192
},
193193
"errorMessageDoesNotSeemToExist": "That message does not seem to exist.",
@@ -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} cannot be authenticated. Please logout and try again.",
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ abstract class ZulipLocalizations {
359359
///
360360
/// In en, this message translates to:
361361
/// **'Could not connect'**
362-
String get errorLoginCouldNotConnectTitle;
362+
String get errorCouldNotConnectTitle;
363363

364364
/// Error message when loading a message that does not exist.
365365
///
@@ -721,6 +721,12 @@ abstract class ZulipLocalizations {
721721
/// **'Topics are required in this organization.'**
722722
String get topicValidationErrorMandatoryButEmpty;
723723

724+
/// Error message in the dialog for invalid API key.
725+
///
726+
/// In en, this message translates to:
727+
/// **'Your account at {url} cannot be authenticated. Please logout and try again.'**
728+
String errorInvalidApiKeyMessage(String url);
729+
724730
/// Error message when an API call returned an invalid response.
725731
///
726732
/// In en, this message translates to:

lib/generated/l10n/zulip_localizations_ar.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ZulipLocalizationsAr extends ZulipLocalizations {
159159
}
160160

161161
@override
162-
String get errorLoginCouldNotConnectTitle => 'Could not connect';
162+
String get errorCouldNotConnectTitle => 'Could not connect';
163163

164164
@override
165165
String get errorMessageDoesNotSeemToExist => 'That message does not seem to exist.';
@@ -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 cannot be authenticated. Please logout and try again.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_en.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ZulipLocalizationsEn extends ZulipLocalizations {
159159
}
160160

161161
@override
162-
String get errorLoginCouldNotConnectTitle => 'Could not connect';
162+
String get errorCouldNotConnectTitle => 'Could not connect';
163163

164164
@override
165165
String get errorMessageDoesNotSeemToExist => 'That message does not seem to exist.';
@@ -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 cannot be authenticated. Please logout and try again.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_fr.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ZulipLocalizationsFr extends ZulipLocalizations {
159159
}
160160

161161
@override
162-
String get errorLoginCouldNotConnectTitle => 'Could not connect';
162+
String get errorCouldNotConnectTitle => 'Could not connect';
163163

164164
@override
165165
String get errorMessageDoesNotSeemToExist => 'That message does not seem to exist.';
@@ -357,6 +357,11 @@ class ZulipLocalizationsFr 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 cannot be authenticated. Please logout and try again.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_ja.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ZulipLocalizationsJa extends ZulipLocalizations {
159159
}
160160

161161
@override
162-
String get errorLoginCouldNotConnectTitle => 'Could not connect';
162+
String get errorCouldNotConnectTitle => 'Could not connect';
163163

164164
@override
165165
String get errorMessageDoesNotSeemToExist => 'That message does not seem to exist.';
@@ -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 cannot be authenticated. Please logout and try again.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_pl.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ZulipLocalizationsPl extends ZulipLocalizations {
159159
}
160160

161161
@override
162-
String get errorLoginCouldNotConnectTitle => 'Nie można połączyć';
162+
String get errorCouldNotConnectTitle => 'Could not connect';
163163

164164
@override
165165
String get errorMessageDoesNotSeemToExist => 'Taka wiadomość raczej nie istnieje.';
@@ -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 cannot be authenticated. Please logout and try again.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'Nieprawidłowa odpowiedź serwera';
362367

lib/generated/l10n/zulip_localizations_ru.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ZulipLocalizationsRu extends ZulipLocalizations {
159159
}
160160

161161
@override
162-
String get errorLoginCouldNotConnectTitle => 'Could not connect';
162+
String get errorCouldNotConnectTitle => 'Could not connect';
163163

164164
@override
165165
String get errorMessageDoesNotSeemToExist => 'That message does not seem to exist.';
@@ -357,6 +357,11 @@ class ZulipLocalizationsRu 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 cannot be authenticated. Please logout and try again.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/log.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ bool debugLog(String message) {
3131
return true;
3232
}
3333

34-
typedef ReportErrorCallback = void Function(String? message, {String? details});
34+
typedef ReportErrorCancellablyCallback = void Function(String? message, {String? details});
35+
typedef ReportErrorCallback = void Function(String message, {String? details});
3536

3637
/// Show the user an error message, without requiring them to interact with it.
3738
///
@@ -48,7 +49,18 @@ typedef ReportErrorCallback = void Function(String? message, {String? details});
4849
// This gets set in [ZulipApp]. We need this indirection to keep `lib/log.dart`
4950
// from importing widget code, because the file is a dependency for the rest of
5051
// the app.
51-
ReportErrorCallback reportErrorToUserBriefly = defaultReportErrorToUserBriefly;
52+
ReportErrorCancellablyCallback reportErrorToUserBriefly = defaultReportErrorToUserBriefly;
53+
54+
/// Show the user a dismissable error message in a modal popup.
55+
///
56+
/// Typically this shows a [AlertDialog] containing the message.
57+
/// If called before the app's widget tree is ready (see [ZulipApp.ready]),
58+
/// then we give up on showing the message to the user,
59+
/// and just log the message to the console.
60+
// This gets set in [ZulipApp]. We need this indirection to keep `lib/log.dart`
61+
// from importing widget code, because the file is a dependency for the rest of
62+
// the app.
63+
ReportErrorCallback reportErrorToUserModally = defaultReportErrorToUserBriefly;
5264

5365
void defaultReportErrorToUserBriefly(String? message, {String? details}) {
5466
// Error dismissing is a no-op to the default handler.

lib/model/store.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,16 @@ class UpdateMachine {
912912
// at 1 kiB (at least on Android), and stack can be longer than that.
913913
assert(debugLog('Stack:\n$s'));
914914
assert(debugLog('Backing off, then will retry…'));
915-
// TODO tell user if initial-fetch errors persist, or look non-transient
915+
// TODO(#890): tell user if initial-fetch errors persist, or look non-transient
916+
switch (e) {
917+
case ZulipApiException(code: 'INVALID_API_KEY'):
918+
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
919+
reportErrorToUserModally(
920+
zulipLocalizations.errorCouldNotConnectTitle,
921+
details: zulipLocalizations.errorInvalidApiKeyMessage(
922+
connection.realmUrl.toString()));
923+
rethrow;
924+
}
916925
await (backoffMachine ??= BackoffMachine()).wait();
917926
assert(debugLog('… Backoff wait complete, retrying initial fetch.'));
918927
}

0 commit comments

Comments
 (0)