### Question:
JsonErrorResponseHandler implements HttpResponseHandler<AmazonServiceException> { @Override public AmazonServiceException handle(HttpResponse response) throws Exception { JsonErrorResponse error; try { error = JsonErrorResponse.fromResponse(response); } catch (IOException e) { throw new AmazonClientException("Unable to parse error response", e); } AmazonServiceException ase = runErrorUnmarshallers(error); if (ase == null) return null; ase.setStatusCode(response.getStatusCode()); if (response.getStatusCode() < HTTP_STATUS_INTERNAL_SERVER_ERROR) { ase.setErrorType(ErrorType.Client); } else { ase.setErrorType(ErrorType.Service); } ase.setErrorCode(error.getErrorCode()); for (Entry<String, String> headerEntry : response.getHeaders().entrySet()) { if ("X-Amzn-RequestId".equalsIgnoreCase(headerEntry.getKey())) { ase.setRequestId(headerEntry.getValue()); } } return ase; } JsonErrorResponseHandler(List<? extends JsonErrorUnmarshaller> exceptionUnmarshallers); @Override AmazonServiceException handle(HttpResponse response); @Override boolean needsConnectionLeftOpen();  }### Answer:
@Test public void testRestJsonErrorResponse() throws Exception { String body = "{\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .header("x-amzn-ErrorType", "InvalidSignatureException") .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@Test public void testJsonErrorResponse() throws Exception { String body = "{\"__type\":\"InvalidSignatureException\",\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@Test public void testJsonErrorResponseReturnsNullIfExceptionIsNull() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return null; } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(403) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); assertNull(handler.handle(response)); }
@Test public void testJsonErrorResponseReturnsServiceErrorTypeIfErrorStatus5XX() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getErrorType(), ErrorType.Service); }
@Test public void testJsonErrorResponseReturnsXAmzRequestId() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .header("X-Amzn-RequestId", "55") .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getRequestId(), "55"); }