public void handleError(HttpCommand command, HttpResponse response) {
   Exception exception = new HttpResponseException(command, response);
   try {
     AtmosStorageError error = parseErrorFromContentOrNull(command, response);
     if (error != null && error.getCode() == 1016) {
       File file = new File(command.getRequest().getEndpoint().getPath());
       exception =
           new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file.getName());
     } else {
       switch (response.getStatusCode()) {
         case 401:
           exception =
               new AuthorizationException(
                   command.getRequest(),
                   error != null ? error.getMessage() : response.getStatusLine());
           break;
         case 404:
           if (!command.getRequest().getMethod().equals("DELETE")) {
             String message =
                 error != null
                     ? error.getMessage()
                     : String.format(
                         "%s -> %s",
                         command.getRequest().getRequestLine(), response.getStatusLine());
             String path = command.getRequest().getEndpoint().getPath();
             Matcher matcher = DIRECTORY_PATH.matcher(path);
             if (matcher.find()) {
               exception = new ContainerNotFoundException(matcher.group(1), message);
             } else {
               matcher = DIRECTORY_KEY_PATH.matcher(path);
               if (matcher.find()) {
                 exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message);
               }
             }
           }
           break;
         default:
           exception =
               error != null
                   ? new AtmosStorageResponseException(command, response, error)
                   : new HttpResponseException(command, response);
       }
     }
   } finally {
     Closeables.closeQuietly(response.getContent());
     command.setException(exception);
   }
 }
  public void handleError(HttpCommand command, HttpResponse response) {
    // it is important to always read fully and close streams
    byte[] data = closeClientButKeepContentStream(response);
    String message = data != null ? new String(data) : null;

    Exception exception =
        message != null
            ? new HttpResponseException(command, response, message)
            : new HttpResponseException(command, response);
    message =
        message != null
            ? message
            : String.format(
                "%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine());
    switch (response.getStatusCode()) {
      case 400:
        break;
      case 401:
      case 403:
        exception = new AuthorizationException(message, exception);
        break;
      case 404:
        if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
          exception = new ResourceNotFoundException(message, exception);
        }
        break;
    }
    command.setException(exception);
  }
    public HttpResponse call() throws Exception {

      HttpResponse response = null;
      for (; ; ) {
        HttpRequest request = command.getCurrentRequest();
        Q nativeRequest = null;
        try {
          for (HttpRequestFilter filter : request.getFilters()) {
            request = filter.filter(request);
          }
          checkRequestHasContentLengthOrChunkedEncoding(
              request,
              "After filtering, the request has neither chunked encoding nor content length: "
                  + request);
          logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine());
          wirePayloadIfEnabled(wire, request);
          utils.logRequest(headerLog, request, ">>");
          nativeRequest = convert(request);
          response = invoke(nativeRequest);

          logger.debug("Receiving response %s: %s", request.hashCode(), response.getStatusLine());
          utils.logResponse(headerLog, response, "<<");
          if (response.getPayload() != null && wire.enabled()) wire.input(response);
          int statusCode = response.getStatusCode();
          if (statusCode >= 300) {
            if (shouldContinue(response)) continue;
            else break;
          } else {
            break;
          }
        } catch (Exception e) {
          IOException ioe = Throwables2.getFirstThrowableOfType(e, IOException.class);
          if (ioe != null) {
            if (ioe instanceof SSLException) {
              command.setException(
                  new AuthorizationException(
                      e.getMessage()
                          + " connecting to "
                          + command.getCurrentRequest().getRequestLine(),
                      e));
              break;
            } else if (ioRetryHandler.shouldRetryRequest(command, ioe)) {
              continue;
            }
          }
          command.setException(
              new HttpResponseException(
                  e.getMessage() + " connecting to " + command.getCurrentRequest().getRequestLine(),
                  command,
                  null,
                  e));
          break;
        } finally {
          cleanup(nativeRequest);
        }
      }
      if (command.getException() != null) throw command.getException();
      return response;
    }
  @Override
  public void handleError(final HttpCommand command, final HttpResponse response) {
    // it is important to always read fully and close streams
    String message = parseMessage(response);
    Exception exception =
        message == null
            ? new HttpResponseException(command, response)
            : new HttpResponseException(command, response, message);
    try {
      message =
          message == null
              ? String.format(
                  "%s -> %s",
                  command.getCurrentRequest().getRequestLine(), response.getStatusLine())
              : message;
      switch (response.getStatusCode()) {
        case 400:
          if (message.contains("unauthorized_client")) {
            exception = new AuthorizationException(message, exception);
          } else {
            exception = new IllegalArgumentException(message, exception);
          }
          break;
        case 401:
        case 403:
          exception = new AuthorizationException(message, exception);
          break;

        case 404:
          if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
            exception = new ResourceNotFoundException(message, exception);
          }
          break;
        case 409:
          exception = new IllegalStateException(message, exception);
          break;
        case 429:
          exception = new AzureComputeRateLimitExceededException(response, exception);
          break;
        default:
      }
    } finally {
      Closeables2.closeQuietly(response.getPayload());
      command.setException(exception);
    }
  }
  @Override
  public void handleError(final HttpCommand command, final HttpResponse response) {
    Exception exception = null;
    String defaultMessage =
        String.format(
            "%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine());

    try {
      switch (response.getStatusCode()) {
        case 401:
        case 403:
          // Authorization exceptions do not return an errors DTO, so we
          // encapsulate a generic exception
          exception =
              new AuthorizationException(
                  defaultMessage, new HttpResponseException(command, response, defaultMessage));
          break;
        case 404:
          // TODO: get the exception to encapsulate from the returned error
          // object
          exception = new ResourceNotFoundException(defaultMessage);
          break;
        case 301:
          // Moved resources in Abiquo should be handled with the
          // ReturnMovedResource exception parser to return the moved
          // entity.
          exception = new HttpResponseException(command, response, defaultMessage);
          break;
        default:
          // TODO: get the exception to encapsulate from the returned error
          // object
          exception = new HttpResponseException(response.getMessage(), command, response);
          break;
      }
    } finally {
      closeQuietly(response.getPayload());
      command.setException(exception);
    }
  }
  public void handleError(HttpCommand command, HttpResponse response) {
    HttpRequest request = command.getCurrentRequest();
    Exception exception = new HttpResponseException(command, response);
    try {
      VCloudError error = null;
      String message = null;
      if (response.getPayload() != null) {
        try {
          error = utils.parseErrorFromContent(request, response);
          if (error != null) {
            message = error.getMessage();
            exception = new VCloudResponseException(command, response, error);
          } else {
            message = Strings2.toStringAndClose(response.getPayload().getInput());
            exception =
                message != null ? new HttpResponseException(command, response, message) : exception;
          }
        } catch (IOException e) {
        } finally {
          response.getPayload().release();
        }
      }
      message =
          message != null
              ? message
              : String.format("%s -> %s", request.getRequestLine(), response.getStatusLine());

      switch (response.getStatusCode()) {
        case 400:
          if (error != null
              && ((error.getMinorErrorCode() != null
                      && error.getMinorErrorCode() == MinorCode.BUSY_ENTITY)
                  || (error.getMessage() != null
                      && error.getMessage().indexOf("is not running") != -1)))
            exception = new IllegalStateException(message, exception);
          else exception = new IllegalArgumentException(message, exception);
          break;
        case 401:
        case 403:
          if (error != null
              && ((error.getMinorErrorCode() != null
                      && error.getMinorErrorCode() == MinorCode.ACCESS_TO_RESOURCE_IS_FORBIDDEN)
                  || (error.getMessage() != null
                      && error.getMessage().indexOf("No access to entity") != -1)))
            exception = new ResourceNotFoundException(message, exception);
          else exception = new AuthorizationException(exception.getMessage(), exception);
          break;
        case 404:
          if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
            String path = command.getCurrentRequest().getEndpoint().getPath();
            Matcher matcher = RESOURCE_PATTERN.matcher(path);
            if (matcher.find()) {
              message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
            } else {
              message = path;
            }
            exception = new ResourceNotFoundException(message);
          }
          break;
      }
    } finally {
      releasePayload(response);
      command.setException(exception);
    }
  }