handleDioError method

ErrorResponse handleDioError(
  1. DioException dioError
)

Implementation

ErrorResponse handleDioError(DioException dioError) {
  switch (dioError.type) {
    case DioExceptionType.cancel:

      errorResponse.message = "Request to API server was cancelled";
      break;
    case DioExceptionType.connectionTimeout:
      errorResponse.message = "Connection timeout with API server";
      break;
    case DioExceptionType.unknown:

      if((dioError.message?.contains("RedirectException")??false)){
        errorResponse.message = "${dioError.message}";
      }else {
        errorResponse.message = "Please check the internet connection";
      }
      break;
    case DioExceptionType.receiveTimeout:

      errorResponse.message = "Receive timeout in connection with API server";
      break;
    case DioExceptionType.badResponse:
      try {

        if (dioError.response?.data['message'] != null) {
          errorResponse.message = dioError.response?.data['message'];
        } else {
          if ((dioError.response?.statusMessage ?? "").isNotEmpty)
            errorResponse.message = dioError.response?.statusMessage;
          else
            return _handleError(
                dioError.response!.statusCode, dioError.response!.data);
        }
      } catch (e) {

        if ((dioError.response?.statusMessage ?? "").isNotEmpty)
          errorResponse.message = dioError.response?.statusMessage;
        else
          return _handleError(
              dioError.response!.statusCode, dioError.response!.data);
      }

      break;
    case DioExceptionType.sendTimeout:

      errorResponse.message = "Send timeout in connection with API server";
      break;
    default:

      errorResponse.message = "Something went wrong";
      break;
  }
  return errorResponse;
}