onError method
The callback will be executed on error.
If you want to continue the error , call handler.next
.
If you want to complete the response with some custom data directly,
you can resolve a Response
object with handler.resolve
and other
error interceptor(s) will be skipped.
If you want to complete the response with an error message directly,
you can reject a DioError
object with handler.reject
, and other
error interceptor(s) will be skipped.
Implementation
@override
onError(DioError err) async {
var extra = RetryOptions.fromExtra(err.request!) ?? this.options;
// var shouldRetry = extra.retries > 0 && await extra.retryEvaluator(err); (bugged, as per https://github.com/aloisdeniel/dio_retry/pull/5)
var shouldRetry = extra.retries > 0 && await options.retryEvaluator(err);
if (shouldRetry) {
if (extra.retryInterval.inMilliseconds > 0) {
await Future<void>.delayed(extra.retryInterval);
}
// Update options to decrease retry count before new try
extra = extra.copyWith(retries: extra.retries - 1);
err.request!.extra = err.request!.extra..addAll(extra.toExtra());
try {
errorCallback!(ErrorResult(err.request!.uri.toString(), err.message, extra.retries, err.error.toString()));
// logger?.warning(
// "[${err.request.uri}] An error occured during request, trying a again (remaining tries: ${extra.retries}, error: ${err.error})");
// We retry with the updated options
return await dio.request<dynamic>(
err.request!.path,
cancelToken: err.request!.cancelToken,
data: err.request!.data,
onReceiveProgress: err.request!.onReceiveProgress,
onSendProgress: err.request!.onSendProgress,
queryParameters: err.request!.queryParameters,
options: Options(extra: err.request!.extra),
);
} catch (e) {
return e;
}
}
return super.onError(err);
}