onError method
- DioException err,
- ErrorInterceptorHandler handler
override
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 DioException object with handler.reject
, and other
error interceptor(s) will be skipped.
Implementation
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
log('[ Error ] $err');
log('[ Error Response] ${err.response}');
if (err.response != null) {
switch (err.response?.statusCode) {
case 400:
throw BadRequestException(err.requestOptions);
case 401:
if (err.response?.headers.value("x-auth-error") ==
"InvalidCredentials") {
throw InvalidCredentialsException(err.requestOptions);
}
throw UnauthorizedException(err.requestOptions);
case 403:
throw UnauthorizedException(err.requestOptions);
case 404:
throw NotFoundException(err.requestOptions);
case 409:
throw ConflictException(err.requestOptions);
case 500:
throw InternalServerErrorException(err.requestOptions);
}
}
switch (err.type) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.sendTimeout:
case DioExceptionType.receiveTimeout:
throw DeadlineExceededException(err.requestOptions);
case DioExceptionType.cancel:
break;
case DioExceptionType.unknown:
throw NoInternetConnectionException(err.requestOptions);
case DioExceptionType.badCertificate:
throw BadCertificateException(err.requestOptions);
case DioExceptionType.badResponse:
throw BadResponseException(err.requestOptions);
case DioExceptionType.connectionError:
throw ConnectionErrorException(err.requestOptions);
}
return handler.next(err);
}