Skip to content

Commit b97601f

Browse files
committed
store [nfc]: Pull out _triagePollRequestError from poll method
1 parent c507a10 commit b97601f

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

lib/model/store.dart

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,45 +1031,8 @@ class UpdateMachine {
10311031
if (_disposed) return;
10321032
store.isLoading = true;
10331033

1034-
if (e is! ApiRequestException) {
1035-
// Some unexpected error, outside even making the HTTP request.
1036-
// Definitely a bug in our code.
1037-
rethrow;
1038-
}
1039-
1040-
bool shouldReportToUser;
1041-
switch (e) {
1042-
case NetworkException(cause: SocketException()):
1043-
// A [SocketException] is common when the app returns from sleep.
1044-
shouldReportToUser = false;
1045-
1046-
case NetworkException():
1047-
case Server5xxException():
1048-
shouldReportToUser = true;
1049-
1050-
case ServerException(httpStatus: 429):
1051-
case ZulipApiException(httpStatus: 429):
1052-
case ZulipApiException(code: 'RATE_LIMIT_HIT'):
1053-
// TODO(#946) handle rate-limit errors more generally, in ApiConnection
1054-
shouldReportToUser = true;
1055-
1056-
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
1057-
rethrow;
1058-
1059-
case ZulipApiException():
1060-
case MalformedServerResponseException():
1061-
// Either a 4xx we didn't expect, or a malformed response;
1062-
// in either case, a mismatch of the client's expectations to the
1063-
// server's behavior, and therefore a bug in one or the other.
1064-
// TODO(#1054) handle auth failures specifically
1065-
rethrow;
1066-
}
1067-
1068-
assert(debugLog('Transient error polling event queue for $store: $e\n'
1069-
'Backing off, then will retry…'));
1070-
if (shouldReportToUser) {
1071-
_maybeReportToUserTransientError(e);
1072-
}
1034+
final shouldRetry = _triagePollRequestError(e);
1035+
if (!shouldRetry) rethrow;
10731036
await (backoffMachine ??= BackoffMachine()).wait();
10741037
if (_disposed) return;
10751038
assert(debugLog('… Backoff wait complete, retrying poll.'));
@@ -1151,6 +1114,54 @@ class UpdateMachine {
11511114
reportErrorToUserBriefly(null);
11521115
}
11531116

1117+
/// Sort out an error from the network request in [poll].
1118+
///
1119+
/// If the request should be retried, this method returns true,
1120+
/// after reporting the error if appropriate to the user and/or developer.
1121+
/// Otherwise, this method returns false with no side effects.
1122+
bool _triagePollRequestError(Object error) {
1123+
if (error is! ApiRequestException) {
1124+
// Some unexpected error, outside even making the HTTP request.
1125+
// Definitely a bug in our code.
1126+
return false;
1127+
}
1128+
1129+
bool shouldReportToUser;
1130+
switch (error) {
1131+
case NetworkException(cause: SocketException()):
1132+
// A [SocketException] is common when the app returns from sleep.
1133+
shouldReportToUser = false;
1134+
1135+
case NetworkException():
1136+
case Server5xxException():
1137+
shouldReportToUser = true;
1138+
1139+
case ServerException(httpStatus: 429):
1140+
case ZulipApiException(httpStatus: 429):
1141+
case ZulipApiException(code: 'RATE_LIMIT_HIT'):
1142+
// TODO(#946) handle rate-limit errors more generally, in ApiConnection
1143+
shouldReportToUser = true;
1144+
1145+
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
1146+
return false;
1147+
1148+
case ZulipApiException():
1149+
case MalformedServerResponseException():
1150+
// Either a 4xx we didn't expect, or a malformed response;
1151+
// in either case, a mismatch of the client's expectations to the
1152+
// server's behavior, and therefore a bug in one or the other.
1153+
// TODO(#1054) handle auth failures specifically
1154+
return false;
1155+
}
1156+
1157+
assert(debugLog('Transient error polling event queue for $store: $error\n'
1158+
'Backing off, then will retry…'));
1159+
if (shouldReportToUser) {
1160+
_maybeReportToUserTransientError(error);
1161+
}
1162+
return true;
1163+
}
1164+
11541165
/// Sort out an error in [poll].
11551166
///
11561167
/// Reports the error if appropriate to the user and/or developer;

0 commit comments

Comments
 (0)