handleError method

dynamic handleError(
  1. HttpClientResponse? holdRes,
  2. Exception e, [
  3. WebServiceExceptionType? type
])

Implementation

handleError(HttpClientResponse? holdRes, Exception e,
    [WebServiceExceptionType? type]) async {
  if (e is WebServiceException) throw raiseError(holdRes, e);

  var res = e is HttpResponseException ? e.response : holdRes;

  // if (res.bodyUsed)
  //     throw this.raiseError(res, createErrorResponse(res.status, res.statusText, type));

  if (res == null)
    throw raiseError(
        null,
        WebServiceException()
          ..innerException = e
          ..statusCode = 500
          ..statusDescription = e.toString());

  var webEx = WebServiceException()
    ..statusCode = res.statusCode
    ..statusDescription = res.reasonPhrase
    ..type = type;

  try {
    String str = await readFully(res);
    if (!isJsonObject(str)) {
      webEx.responseStatus = createErrorResponse(
              res.statusCode.toString(), res.reasonPhrase, type)
          .responseStatus;
    } else {
      var jsonObj = json.decode(str);
      webEx.responseStatus = createResponseStatus(jsonObj);
    }
  } on Exception catch (e, trace) {
    Log.warn("handleError(): $e\n$trace");
    webEx.innerException = e;
  }

  throw raiseError(res, webEx);
}