unwrapClientCall<T> function

Future<T> unwrapClientCall<T>(
  1. Future<Response<T>> clientCall, {
  2. bool throwOnNon200 = true,
})

Implementation

Future<T> unwrapClientCall<T>(Future<Response<T>> clientCall,
    {bool throwOnNon200 = true}) async {
  Response<T> response;

  try {
    response = await clientCall;
  } catch (e) {
    if (e is DioError) {
      // Add the response data to the error message.
      var newError = DioError(
        error: "${e.message} ${e.response?.data}",
        requestOptions: e.requestOptions,
        response: e.response,
        type: e.type,
        stackTrace: e.stackTrace,
      );
      throw newError;
    }
    rethrow;
  }
  if (response.data == null) {
    throw "Empty response: ${response.statusCode}: ${response.statusMessage}";
  }
  if (throwOnNon200 &&
      response.statusCode != null &&
      (response.statusCode! < 200 || response.statusCode! > 299)) {
    throw "Non-200 response: ${response.statusCode}: ${response.statusMessage}";
  }
  return response.data as T; // Dart prefers this over !
}