### Question:
DefaultErrorResponseHandler implements HttpResponseHandler<AmazonServiceException> { @Override public AmazonServiceException handle(HttpResponse errorResponse) throws Exception { String content = ""; try { content = IOUtils.toString(errorResponse.getContent()); } catch (IOException ex) { if (log.isDebugEnabled()) log.debug("Failed in reading the error response", ex); return newAmazonServiceException( "Unable to unmarshall error response", errorResponse, ex); } Document document; try { document = XpathUtils.documentFrom(content); } catch (Exception e) { return newAmazonServiceException(String.format( "Unable to unmarshall error response (%s)", content), errorResponse, e); } for (Unmarshaller<AmazonServiceException, Node> unmarshaller : unmarshallerList) { AmazonServiceException ase = unmarshaller.unmarshall(document); if (ase != null) { ase.setStatusCode(errorResponse.getStatusCode()); return ase; } } throw new AmazonClientException("Unable to unmarshall error response from service"); } DefaultErrorResponseHandler(
            List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList); @Override AmazonServiceException handle(HttpResponse errorResponse); @Override boolean needsConnectionLeftOpen();  }### Answer:
@Test(expected = AmazonClientException.class) public void testDefaultErrorResponseHandlerNoUsefulUnmarshallers() throws Exception { List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList = new ArrayList<Unmarshaller<AmazonServiceException, Node>>(); String xmlResponse = "<error>TestError</error>"; HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(400) .statusText("Error") .content(new ByteArrayInputStream(xmlResponse.getBytes(StringUtils.UTF8))) .build(); final List<Boolean> called = new ArrayList<Boolean>(); Unmarshaller<AmazonServiceException, Node> incorrectUnmarshaller = new Unmarshaller<AmazonServiceException, Node>() { @Override public AmazonServiceException unmarshall(Node in) throws Exception { called.add(true); return null; } }; unmarshallerList.add(incorrectUnmarshaller); DefaultErrorResponseHandler handler = new DefaultErrorResponseHandler(unmarshallerList); try { handler.handle(errorResponse); } finally { assertEquals(called.size(), 1); assertTrue(called.get(0)); } }
@Test public void testDefaultErrorResponseHandlerCannotGetContent() throws Exception { List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList = new ArrayList<Unmarshaller<AmazonServiceException, Node>>(); HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(400) .statusText("Error") .content(new InputStream() { @Override public int read() throws IOException { throw new IOException("Test IOException"); } }) .build(); DefaultErrorResponseHandler handler = new DefaultErrorResponseHandler(unmarshallerList); AmazonServiceException e = handler.handle(errorResponse); assertEquals(e.getErrorCode(), errorResponse.getStatusCode() + " " + errorResponse.getStatusText()); assertEquals(e.getStatusCode(), 400); assertEquals(e.getErrorType(), ErrorType.Unknown); }