intercept method
Processes request and optionally delegates to next.
Implementation
@override
Future<StreamedApiResponse> intercept(ApiRequest request, Handler next) async {
Object? lastError;
StackTrace? lastStack;
StreamedApiResponse? previousResponse;
for (var attempt = 0; attempt <= maxRetries; attempt++) {
if (request.options?.cancelToken?.isCancelled ?? false) {
throw const CancelledException();
}
if (attempt > 0) {
final delay = _computeDelay(
attempt,
previousResponse: previousResponse,
);
await _sleep(delay);
}
try {
final response = await next(request);
previousResponse = response;
if (!_shouldRetry(response) ||
!_shouldRetryRequest(request) ||
attempt == maxRetries) {
return response;
}
await response.discard();
} on CancelledException {
rethrow;
} on Object catch (e, st) {
lastError = e;
lastStack = st;
if (attempt == maxRetries || !_shouldRetryRequest(request)) break;
}
}
// All retries exhausted with exceptions.
Error.throwWithStackTrace(lastError!, lastStack!);
}