/**
   * Checks if the response status code is the expected and throws the appropriate
   * KubernetesClientException if not.
   *
   * @param r The {@link com.ning.http.client.Response} object.
   * @param expectedStatusCode The expected status code.
   * @throws KubernetesClientException When the response code is not the expected.
   */
  protected void assertResponseCode(Response r, int expectedStatusCode) {
    int statusCode = r.getStatusCode();
    String customMessage = client.getConfiguration().getErrorMessages().get(statusCode);

    if (statusCode == expectedStatusCode) {
      return;
    } else if (customMessage != null) {
      throw new KubernetesClientException(
          "Error accessing: " + r.getUri().toString() + ",due to:" + customMessage);
    } else {
      try {
        Status status = OBJECT_MAPPER.readValue(r.getResponseBodyAsStream(), Status.class);
        throw new KubernetesClientException(status.getMessage(), status.getCode(), status);
      } catch (IOException e) {
        throw new KubernetesClientException(
            e.getMessage(),
            statusCode,
            new StatusBuilder().withCode(statusCode).withMessage(e.getMessage()).build());
      }
    }
  }
  KubernetesClientException requestFailure(Request request, Status status) {
    StringBuilder sb = new StringBuilder();
    sb.append("Failure executing: ")
        .append(request.method())
        .append(" at: ")
        .append(request.urlString())
        .append(". Received status: ")
        .append(status)
        .append(".");

    if (status.getMessage() != null && !status.getMessage().isEmpty()) {
      sb.append(" Message: ").append(status.getMessage()).append(".");
    }

    if (status.getReason() != null && !status.getReason().isEmpty()) {
      sb.append(" Reason: ").append(status.getReason()).append(".");
    }

    return new KubernetesClientException(sb.toString(), status.getCode(), status);
  }