private AmazonS3Exception createExceptionFromHeaders( HttpResponse errorResponse, String errorResponseXml) { final Map<String, String> headers = errorResponse.getHeaders(); final int statusCode = errorResponse.getStatusCode(); final AmazonS3ExceptionBuilder exceptionBuilder = new AmazonS3ExceptionBuilder(); exceptionBuilder.setErrorMessage(errorResponse.getStatusText()); exceptionBuilder.setErrorResponseXml(errorResponseXml); exceptionBuilder.setStatusCode(statusCode); exceptionBuilder .setExtendedRequestId(headers.get(Headers.EXTENDED_REQUEST_ID)); exceptionBuilder.setRequestId(headers.get(Headers.REQUEST_ID)); exceptionBuilder.setCloudFrontId(headers.get(Headers.CLOUD_FRONT_ID)); exceptionBuilder .setErrorCode(statusCode + " " + errorResponse.getStatusText()); exceptionBuilder.addAdditionalDetail(Headers.S3_BUCKET_REGION, errorResponse.getHeaders().get(Headers.S3_BUCKET_REGION)); return exceptionBuilder.build(); }
@Override public AmazonServiceException handle(HttpResponse errorResponse) throws Exception { AmazonServiceException ase = createAse(errorResponse); if (ase == null) { throw new SdkClientException("Unable to unmarshall error response from service"); } ase.setHttpHeaders(errorResponse.getHeaders()); if (StringUtils.isNullOrEmpty(ase.getErrorCode())) { ase.setErrorCode(errorResponse.getStatusCode() + " " + errorResponse.getStatusText()); } return ase; }
@Override public T handle(HttpResponse response) throws Exception { final AmazonWebServiceResponse<T> awsResponse = delegate.handle(response); if (awsResponse == null) { throw new RuntimeException("Unable to unmarshall response metadata. Response Code: " + response.getStatusCode() + ", Response Text: " + response.getStatusText()); } AmazonWebServiceRequest userRequest = request.getOriginalRequest(); if (userRequest.getCloneRoot() != null) { userRequest = userRequest.getCloneRoot(); } responseMetadataCache.add(userRequest, awsResponse.getResponseMetadata()); final String awsRequestId = awsResponse.getRequestId(); if (requestLog.isDebugEnabled()) { requestLog .debug("Received successful response: " + response.getStatusCode() + ", AWS Request ID: " + awsRequestId); } if (!logHeaderRequestId(response)) { // Logs the AWS request ID extracted from the payload if // it is not available from the response header. logResponseRequestId(awsRequestId); } awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, awsRequestId); return fillInResponseMetadata(awsResponse, response); }
"Unable to unmarshall response (" + e.getMessage() + "). Response Code: " + httpResponse.getStatusCode() + ", Response Text: " + httpResponse.getStatusText(); throw new SdkClientException(errorMessage, e);
/** * Returns the HTTP status text associated with this response. * * @return The HTTP status text associated with this response. */ public String getStatusText() { return response.getStatusText(); }
private AmazonServiceException handleAse(HttpResponse response) throws Exception { final int statusCode = response.getStatusCode(); try { return delegate.handle(response); } catch(InterruptedException e) { throw e; } catch (Exception e) { // If the errorResponseHandler doesn't work, then check for error responses that don't have any content if (statusCode == 413) { AmazonServiceException exception = new AmazonServiceException("Request entity too large"); exception.setServiceName(response.getRequest().getServiceName()); exception.setStatusCode(statusCode); exception.setErrorType(AmazonServiceException.ErrorType.Client); exception.setErrorCode("Request entity too large"); return exception; } else if (statusCode >= 500 && statusCode < 600) { AmazonServiceException exception = new AmazonServiceException(response.getStatusText()); exception.setServiceName(response.getRequest().getServiceName()); exception.setStatusCode(statusCode); exception.setErrorType(AmazonServiceException.ErrorType.Service); exception.setErrorCode(response.getStatusText()); return exception; } else { throw e; } } }
statusExplanation = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, null); } else { statusExplanation = response.getHttpResponse().getStatusText();
/** * Used to create an {@link AmazonS3Exception} when we failed to read the * error response or parsed the error response as XML. */ private AmazonS3Exception newAmazonS3Exception(String errmsg, HttpResponse httpResponse) { final AmazonS3Exception ase = new AmazonS3Exception(errmsg); final int statusCode = httpResponse.getStatusCode(); ase.setErrorCode(statusCode + " " + httpResponse.getStatusText()); ase.setStatusCode(statusCode); ase.setErrorType(errorTypeOf(statusCode)); final Map<String, String> headers = httpResponse.getHeaders(); ase.setRequestId(headers.get(Headers.REQUEST_ID)); ase.setExtendedRequestId(headers.get(Headers.EXTENDED_REQUEST_ID)); ase.setCloudFrontId(headers.get(Headers.CLOUD_FRONT_ID)); final Map<String, String> additionalDetails = new HashMap<String, String>(); additionalDetails.put(Headers.S3_BUCKET_REGION, headers.get(Headers.S3_BUCKET_REGION)); ase.setAdditionalDetails(additionalDetails); return ase; }
@Override public AmazonServiceException handle(HttpResponse errorResponse) throws Exception { AmazonServiceException ase = createAse(errorResponse); if (ase == null) { throw new SdkClientException("Unable to unmarshall error response from service"); } ase.setHttpHeaders(errorResponse.getHeaders()); if (StringUtils.isNullOrEmpty(ase.getErrorCode())) { ase.setErrorCode(errorResponse.getStatusCode() + " " + errorResponse.getStatusText()); } return ase; }
/** * Used to create an {@link newAmazonServiceException} when we failed to * read the error response or parsed the error response as XML. */ private AmazonServiceException newAmazonServiceException(String errmsg, HttpResponse httpResponse, Exception readFailure) { AmazonServiceException exception = new AmazonServiceException(errmsg, readFailure); final int statusCode = httpResponse.getStatusCode(); exception.setErrorCode(statusCode + " " + httpResponse.getStatusText()); exception.setErrorType(AmazonServiceException.ErrorType.Unknown); exception.setStatusCode(statusCode); return exception; }
"Unable to unmarshall response metadata. Response Code: " + response.getStatusCode() + ", Response Text: " + response.getStatusText()); final String errorMessage = "Unable to unmarshall response (" + e.getMessage() + "). Response Code: " + response.getStatusCode() + ", Response Text: " + response.getStatusText(); throw new AmazonClientException(errorMessage, e);
@Override public T handle(HttpResponse response) throws Exception { final AmazonWebServiceResponse<T> awsResponse = delegate.handle(response); if (awsResponse == null) { throw new RuntimeException("Unable to unmarshall response metadata. Response Code: " + response.getStatusCode() + ", Response Text: " + response.getStatusText()); } AmazonWebServiceRequest userRequest = request.getOriginalRequest(); if (userRequest.getCloneRoot() != null) { userRequest = userRequest.getCloneRoot(); } responseMetadataCache.add(userRequest, awsResponse.getResponseMetadata()); final String awsRequestId = awsResponse.getRequestId(); if (requestLog.isDebugEnabled()) { requestLog .debug("Received successful response: " + response.getStatusCode() + ", AWS Request ID: " + awsRequestId); } if (!logHeaderRequestId(response)) { // Logs the AWS request ID extracted from the payload if // it is not available from the response header. logResponseRequestId(awsRequestId); } awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, awsRequestId); return fillInResponseMetadata(awsResponse, response); }
"Unable to unmarshall response (" + e.getMessage() + "). Response Code: " + httpResponse.getStatusCode() + ", Response Text: " + httpResponse.getStatusText(); throw new SdkClientException(errorMessage, e);
@Override public AmazonServiceException handle( com.amazonaws.http.HttpResponse response) throws Exception { AmazonServiceException ase = new AmazonServiceException("Fake service exception."); ase.setStatusCode(response.getStatusCode()); ase.setErrorCode(response.getStatusText()); return ase; }
@Test public void testCreateResponse() throws URISyntaxException, IOException { final HttpRequest request = new HttpRequest("PUT", new URI("https://www.test.com")); final MockHttpURLConnection conn = new MockHttpURLConnection(new URL("https://www.test.com")); final Map<String, List<String>> headerFields = new HashMap<String, List<String>>(); final List<String> header1 = new ArrayList<String>(); header1.add("value1"); header1.add("ExtraValue"); headerFields.put("key1", header1); final List<String> header2 = new ArrayList<String>(); header2.add("value2"); headerFields.put("key2", header2); final List<String> nullHeader = new ArrayList<String>(); headerFields.put(null, nullHeader); conn.setHeaderFields(headerFields); conn.setResponseCode(200); conn.setErrorStream(null); final ByteArrayInputStream bais = new ByteArrayInputStream("test".getBytes(StringUtils.UTF8)); conn.setInputStream(bais); conn.setResponseMessage("TestMessage"); final HttpResponse response = client.createHttpResponse(request, conn); assertEquals(response.getHeaders().get("key1"), "value1"); assertEquals(response.getHeaders().get("key2"), "value2"); assertEquals(response.getHeaders().size(), 2); assertEquals(response.getStatusCode(), 200); assertEquals(response.getStatusText(), "TestMessage"); assertSame(response.getContent(), bais); }
private AmazonServiceException handleAse(HttpResponse response) throws Exception { final int statusCode = response.getStatusCode(); try { return delegate.handle(response); } catch(InterruptedException e) { throw e; } catch (Exception e) { // If the errorResponseHandler doesn't work, then check for error responses that don't have any content if (statusCode == 413) { AmazonServiceException exception = new AmazonServiceException("Request entity too large"); exception.setServiceName(response.getRequest().getServiceName()); exception.setStatusCode(statusCode); exception.setErrorType(AmazonServiceException.ErrorType.Client); exception.setErrorCode("Request entity too large"); return exception; } else if (statusCode >= 500 && statusCode < 600) { AmazonServiceException exception = new AmazonServiceException(response.getStatusText()); exception.setServiceName(response.getRequest().getServiceName()); exception.setStatusCode(statusCode); exception.setErrorType(AmazonServiceException.ErrorType.Service); exception.setErrorCode(response.getStatusText()); return exception; } else { throw e; } } }
exception.setErrorCode("Request entity too large"); } else if (status == HTTP_STATUS_SERVICE_UNAVAILABLE && "Service Unavailable".equalsIgnoreCase(response.getStatusText())) { exception = new AmazonServiceException("Service unavailable"); exception.setServiceName(request.getServiceName()); final String errorMessage = "Unable to unmarshall error response (" + e.getMessage() + "). Response Code: " + status + ", Response Text: " + response.getStatusText() + ", Response Headers: " + response.getHeaders(); throw new AmazonClientException(errorMessage, e);
return newAmazonS3Exception(errorResponse.getStatusText(), errorResponse); log.debug("Failed in reading the error response", ex); return newAmazonS3Exception(errorResponse.getStatusText(), errorResponse);
@Test public void testBuilder() throws Exception { builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); for (int i = 0; i < 10; i++) { builder.header("key" + i, "value" + i); } response = builder.build(); assertEquals("status text", statusText, response.getStatusText()); assertTrue("status code", statusCode == response.getStatusCode()); assertTrue("has headers", 10 == response.getHeaders().size()); assertEquals("content", content, response.getContent()); }
@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); }