Skip to content

Commit b28eb28

Browse files
committed
store [nfc]: Factor out report-and-retry from switch statement
This will make it easier to add more cases here without making the logic complicated to follow.
1 parent 59609d7 commit b28eb28

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

lib/model/store.dart

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,8 @@ class UpdateMachine {
10341034
if (_disposed) return;
10351035

10361036
store.isLoading = true;
1037+
bool isUnexpected;
1038+
bool shouldReportToUser;
10371039
switch (e) {
10381040
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
10391041
assert(debugLog('Lost event queue for $store. Replacing…'));
@@ -1042,31 +1044,38 @@ class UpdateMachine {
10421044
assert(debugLog('… Event queue replaced.'));
10431045
return;
10441046

1047+
case NetworkException(cause: SocketException()):
1048+
// A [SocketException] is common when the app returns from sleep.
1049+
isUnexpected = false;
1050+
shouldReportToUser = false;
1051+
10451052
case NetworkException():
10461053
case Server5xxException():
1047-
assert(debugLog('Transient error polling event queue for $store: $e\n'
1048-
'Backing off, then will retry…'));
1049-
if (e is NetworkException && e.cause is SocketException) {
1050-
// The error is boring; skip reporting it to the user.
1051-
// A [SocketException] is common when the app returns from sleep.
1052-
} else {
1053-
maybeReportToUserTransientError(e);
1054-
}
1055-
await (backoffMachine ??= BackoffMachine()).wait();
1056-
if (_disposed) return;
1057-
assert(debugLog('… Backoff wait complete, retrying poll.'));
1058-
continue;
1054+
isUnexpected = false;
1055+
shouldReportToUser = true;
10591056

10601057
default:
1061-
assert(debugLog('Error polling event queue for $store: $e\n'
1062-
'Backing off and retrying even though may be hopeless…'));
1063-
// TODO(#186): Handle unrecoverable failures
1064-
_reportToUserErrorConnectingToServer(e);
1065-
await (backoffMachine ??= BackoffMachine()).wait();
1066-
if (_disposed) return;
1067-
assert(debugLog('… Backoff wait complete, retrying poll.'));
1068-
continue;
1058+
isUnexpected = true;
1059+
shouldReportToUser = true;
1060+
}
1061+
1062+
if (isUnexpected) {
1063+
assert(shouldReportToUser);
1064+
assert(debugLog('Error polling event queue for $store: $e\n'
1065+
'Backing off and retrying even though may be hopeless…'));
1066+
// TODO(#186): Handle unrecoverable failures
1067+
_reportToUserErrorConnectingToServer(e);
1068+
} else {
1069+
assert(debugLog('Transient error polling event queue for $store: $e\n'
1070+
'Backing off, then will retry…'));
1071+
if (shouldReportToUser) {
1072+
maybeReportToUserTransientError(e);
1073+
}
10691074
}
1075+
await (backoffMachine ??= BackoffMachine()).wait();
1076+
if (_disposed) return;
1077+
assert(debugLog('… Backoff wait complete, retrying poll.'));
1078+
continue;
10701079
}
10711080

10721081
// After one successful request, we reset backoff to its initial state.

0 commit comments

Comments
 (0)