onError method
Called when an exception was occurred during the request.
Implementation
@override
Future<dynamic> onError(
DioException err,
ErrorInterceptorHandler handler,
) async {
if (err.requestOptions.disableRetry) {
return super.onError(err, handler);
}
bool isRequestCancelled() =>
err.requestOptions.cancelToken?.isCancelled ?? false;
final attempt = err.requestOptions._attempt + 1;
final shouldRetry = attempt <= retries && await _shouldRetry(err, attempt);
if (!shouldRetry) {
return super.onError(err, handler);
}
err.requestOptions._attempt = attempt;
final delay = _getDelay(attempt);
logPrint?.call(
'[${err.requestOptions.path}] An error occurred during request, '
'trying again '
'(attempt: $attempt/$retries, '
'wait ${delay.inMilliseconds} ms, '
'error: ${err.error ?? err})',
);
var requestOptions = err.requestOptions;
if (requestOptions.data is FormData) {
try {
requestOptions = _recreateOptions(err.requestOptions);
} on RetryNotSupportedException catch (e) {
return super.onError(
DioException(requestOptions: requestOptions, error: e),
handler,
);
}
}
if (delay != Duration.zero) {
await Future<void>.delayed(delay);
}
if (isRequestCancelled()) {
logPrint?.call('Request was cancelled. Cancel retrying.');
return super.onError(err, handler);
}
try {
await dio
.fetch<void>(requestOptions)
.then((value) => handler.resolve(value));
} on DioException catch (e) {
super.onError(e, handler);
}
}