NovaErrorHandler implements HttpErrorHandler { public void handleError(HttpCommand command, HttpResponse response) { byte[] data = closeClientButKeepContentStream(response); String content = data != null ? emptyToNull(new String(data)) : null; Exception exception = content != null ? new HttpResponseException(command, response, content) : new HttpResponseException(command, response); String requestLine = command.getCurrentRequest().getRequestLine(); String message = content != null ? content : String.format("%s -> %s", requestLine, response.getStatusLine()); switch (response.getStatusCode()) { case 400: if (message.indexOf("quota exceeded") != -1) exception = new InsufficientResourcesException(message, exception); else if (message.indexOf("has no fixed_ips") != -1) exception = new IllegalStateException(message, exception); else if (message.indexOf("already exists") != -1) exception = new IllegalStateException(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 413: if (content == null) { exception = new InsufficientResourcesException(message, exception); break; } exception = parseAndBuildRetryException(content, message, exception); } command.setException(exception); } protected  NovaErrorHandler(HeaderToRetryAfterException retryAfterParser, OverLimitParser overLimitParser); @Inject  NovaErrorHandler(DateCodecFactory factory, OverLimitParser overLimitParser); void handleError(HttpCommand command, HttpResponse response);  }
@Test public void test401MakesAuthorizationException() { fn.handleError(command, HttpResponse.builder().statusCode(401).message("Unauthorized").build()); assertEquals(command.getException().getClass(), AuthorizationException.class); assertEquals(command.getException().getMessage(), "POST https: }
@Test public void test404MakesResourceNotFoundException() { fn.handleError(command, HttpResponse.builder().statusCode(404).message("Not Found").build()); assertEquals(command.getException().getClass(), ResourceNotFoundException.class); assertEquals(command.getException().getMessage(), "POST https: }
@Test public void test400MakesIllegalStateExceptionOnQuotaExceededOnNoFixedIps() { fn.handleError(command, noFixedIps); assertEquals(command.getException().getClass(), IllegalStateException.class); assertEquals(command.getException().getMessage(), noFixedIps.getPayload().getRawContent()); }
@Test public void test400MakesIllegalStateExceptionOnAlreadyExists() { fn.handleError(command, alreadyExists); assertEquals(command.getException().getClass(), IllegalStateException.class); assertEquals(command.getException().getMessage(), alreadyExists.getPayload().getRawContent()); }
@Test public void test400MakesInsufficientResourcesExceptionOnQuotaExceeded() { fn.handleError(command, quotaExceeded); assertEquals(command.getException().getClass(), InsufficientResourcesException.class); assertEquals(command.getException().getMessage(), quotaExceeded.getPayload().getRawContent()); }
@Test public void test413MakesInsufficientResourcesException() { fn.handleError(command, tooLarge); assertEquals(command.getException().getClass(), InsufficientResourcesException.class); assertEquals(command.getException().getMessage(), tooLarge.getPayload().getRawContent()); }
@Test public void test413WithRetryAtExceptionParsesDelta() { fn.handleError(command, retryAt); assertEquals(command.getException().getClass(), RetryAfterException.class); assertEquals(command.getException().getMessage(), "retry in 3600 seconds"); }
@Test public void test413WithRetryAfterExceptionFolsom() { fn.handleError(command, retryAfter); assertEquals(command.getException().getClass(), RetryAfterException.class); assertEquals(command.getException().getMessage(), "retry in 54 seconds"); }
@Test public void test413WithRetryAfterTrumpsRetryAt() { fn.handleError(command, retryAfterTrumps); assertEquals(command.getException().getClass(), RetryAfterException.class); assertEquals(command.getException().getMessage(), "retry in 54 seconds"); }
@Test public void test413WithBadRetryAtFormatFallsBack() { fn.handleError(command, badRetryAt); assertEquals(command.getException().getClass(), InsufficientResourcesException.class); assertEquals(command.getException().getMessage(), badRetryAt.getPayload().getRawContent()); }