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
void onError(
DioError err,
ErrorInterceptorHandler handler,
) {
if (err.response != null) {
if ([301, 302].contains(err.response!.statusCode)) {
final redirectCount = (err.requestOptions
.extra['domainFrontingRawOptionsRedirectCount'] ??
0);
final location =
err.response?.headers[HttpHeaders.locationHeader]?.first;
if (location != null) {
if (redirectCount <= 5) {
final uri = err.requestOptions.uri.resolve(location);
final request = err.requestOptions;
final newRequest = request.copyWith(path: uri.path, extra: {
...request.extra,
'domainFrontingRawOptionsRedirectCount': redirectCount + 1,
});
dio.fetch(newRequest).then((value) => handler.resolve(value));
return;
}
}
}
}
handler.next(err);
}