throwIfResponseHasError method

dynamic throwIfResponseHasError()

Implementation

dynamic throwIfResponseHasError() {
  if (!hasError) {
    return this;
  }

  final requestBody = this.body.toString();

  Exception exception;

  switch (this.statusCode) {
    case 400:
      exception = BadRequestExcepiton(requestBody);
      break;
    case 404:
      exception = NotFoundException(this.request?.url.path);
      break;
    case 422:
      exception = UnprocessableEntityException(requestBody);
      break;
    case 401:
    case 403:
      exception = UnauthorisedException(requestBody);
      break;
    case 500:
      exception = FetchDataException('Error occured while communication with Server with Status Code: 500');
      break;
    default:
      {
        var statusText = this.statusText ?? '';
        if (statusText.containsIgnoreCase(ExceptionConstants.SocketException)) {
          exception = SocketException(statusText);
        }

        if (statusText.containsIgnoreCase(ExceptionConstants.TimeoutException)) {
          exception = TimeoutException(statusText);
        }

        exception = UnknownApiException(this.bodyString);
        break;
      }
  }

  throw exception;
}