Route53ErrorHandler implements HttpErrorHandler { public void handleError(HttpCommand command, HttpResponse response) { Exception exception = new HttpResponseException(command, response); try { byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; if (message != null) { exception = new HttpResponseException(command, response, message); if (message.indexOf("ErrorResponse") != -1) { AWSError error = factory.create(handlers.get()).parse(message); exception = refineException(new AWSResponseException(command, response, error)); } else if (message.indexOf("InvalidChangeBatch") != -1) { ImmutableList<String> errors = factory.create(batchHandlers.get()).parse(message); exception = new InvalidChangeBatchException(errors, new HttpResponseException(command, response)); } } } finally { releasePayload(response); command.setException(exception); } } @Inject  Route53ErrorHandler(Factory factory, Provider<ErrorHandler> handlers,
         Provider<InvalidChangeBatchHandler> batchHandlers); void handleError(HttpCommand command, HttpResponse response);  }
@Test public void testInvalidChangeBatchException() throws IOException { HttpResponse response = HttpResponse.builder().statusCode(BAD_REQUEST.getStatusCode()) .payload(payloadFromResource("/invalid_change_batch.xml")).build(); function.handleError(command, response); InvalidChangeBatchException exception = InvalidChangeBatchException.class.cast(command.getException()); assertEquals(exception.getMessages(), ImmutableSet.of( "Tried to create resource record set duplicate.example.com. type A, but it already exists", "Tried to delete resource record set noexist.example.com. type A, but it was not found")); }
@Test public void testDeleteNotFound() throws IOException { HttpResponse response = HttpResponse.builder().statusCode(BAD_REQUEST.getStatusCode()) .payload( payloadFromStringWithContentType( "<ErrorResponse><Error><Type>Sender</Type><Code>InvalidChangeBatch</Code>" + "<Message>Tried to delete resource record set krank.foo.bar., type TXT but it was not found</Message>" + "</Error></ErrorResponse>", "application/xml")).build(); function.handleError(command, response); assertEquals(command.getException().getClass(), ResourceNotFoundException.class); assertEquals(command.getException().getMessage(), "Tried to delete resource record set krank.foo.bar., type TXT but it was not found"); AWSResponseException exception = AWSResponseException.class.cast(command.getException().getCause()); assertEquals(exception.getError().getCode(), "InvalidChangeBatch"); }