private void handleErrorResponse(InputStream errorStream, int statusCode, String responseMessage) throws IOException { String errorCode = null; // Parse the error stream returned from the service. if(errorStream != null) { String errorResponse = IOUtils.toString(errorStream); try { JsonNode node = Jackson.jsonNodeOf(errorResponse); JsonNode code = node.get("code"); JsonNode message = node.get("message"); if (code != null && message != null) { errorCode = code.asText(); responseMessage = message.asText(); } } catch (Exception exception) { LOG.debug("Unable to parse error stream"); } } AmazonServiceException ase = new AmazonServiceException(responseMessage); ase.setStatusCode(statusCode); ase.setErrorCode(errorCode); throw ase; } }
private AmazonServiceException createAse(HttpResponse errorResponse) throws Exception { // Try to parse the error response as XML final Document document = documentFromContent(errorResponse.getContent(), idString(errorResponse)); /* * We need to select which exception unmarshaller is the correct one to * use from all the possible exceptions this operation can throw. * Currently we rely on the unmarshallers to return null if they can't * unmarshall the response, but we might need something a little more * sophisticated in the future. */ for (Unmarshaller<AmazonServiceException, Node> unmarshaller : unmarshallerList) { AmazonServiceException ase = unmarshaller.unmarshall(document); if (ase != null) { ase.setStatusCode(errorResponse.getStatusCode()); return ase; } } return null; }
@Override public AmazonServiceException handle(HttpResponse response) throws Exception { final AmazonServiceException ase = handleAse(response); ase.setStatusCode(response.getStatusCode()); ase.setServiceName(response.getRequest().getServiceName()); awsRequestMetrics.addPropertyWith(AWSRequestMetrics.Field.AWSRequestID, ase.getRequestId()) .addPropertyWith(AWSRequestMetrics.Field.AWSErrorCode, ase.getErrorCode()) .addPropertyWith(AWSRequestMetrics.Field.StatusCode, ase.getStatusCode()); return ase; }
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; } } }
@Override public AmazonServiceException handle(HttpResponse response) throws Exception { JsonContent jsonContent = JsonContent.createJsonContent(response, jsonFactory); String errorCode = errorCodeParser.parseErrorCode(response, jsonContent); AmazonServiceException ase = createException(errorCode, jsonContent); // Jackson has special-casing for 'message' values when deserializing // Throwables, but sometimes the service passes the error message in // other JSON fields - handle it here. if (ase.getErrorMessage() == null) { ase.setErrorMessage(errorMessageParser.parseErrorMessage(response, jsonContent.getJsonNode())); } ase.setErrorCode(errorCode); ase.setServiceName(response.getRequest().getServiceName()); ase.setStatusCode(response.getStatusCode()); ase.setErrorType(getErrorTypeFromStatusCode(response.getStatusCode())); ase.setRawResponse(jsonContent.getRawContent()); String requestId = getRequestIdFromHeaders(response.getHeaders()); if (requestId != null) { ase.setRequestId(requestId); } ase.setHttpHeaders(response.getHeaders()); return ase; }
private void handleErrorResponse(InputStream errorStream, int statusCode, String responseMessage) throws IOException { String errorCode = null; // Parse the error stream returned from the service. if(errorStream != null) { String errorResponse = IOUtils.toString(errorStream); try { JsonNode node = Jackson.jsonNodeOf(errorResponse); JsonNode code = node.get("code"); JsonNode message = node.get("message"); if (code != null && message != null) { errorCode = code.asText(); responseMessage = message.asText(); } } catch (Exception exception) { LOG.debug("Unable to parse error stream"); } } AmazonServiceException ase = new AmazonServiceException(responseMessage); ase.setStatusCode(statusCode); ase.setErrorCode(errorCode); throw ase; } }
private AmazonServiceException createAse(HttpResponse errorResponse) throws Exception { // Try to parse the error response as XML final Document document = documentFromContent(errorResponse.getContent(), idString(errorResponse)); /* * We need to select which exception unmarshaller is the correct one to * use from all the possible exceptions this operation can throw. * Currently we rely on the unmarshallers to return null if they can't * unmarshall the response, but we might need something a little more * sophisticated in the future. */ for (Unmarshaller<AmazonServiceException, Node> unmarshaller : unmarshallerList) { AmazonServiceException ase = unmarshaller.unmarshall(document); if (ase != null) { ase.setStatusCode(errorResponse.getStatusCode()); return ase; } } return null; }
/** * 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; }
@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; }
AmazonServiceException ase = unmarshaller.unmarshall(document); if (ase != null) { ase.setStatusCode(errorResponse.getStatusCode()); return ase;
@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; }
exception = new AmazonServiceException("Request entity too large"); exception.setServiceName(request.getServiceName()); exception.setStatusCode(HTTP_STATUS_REQ_TOO_LONG); exception.setErrorType(ErrorType.Client); exception.setErrorCode("Request entity too large"); exception = new AmazonServiceException("Service unavailable"); exception.setServiceName(request.getServiceName()); exception.setStatusCode(HTTP_STATUS_SERVICE_UNAVAILABLE); exception.setErrorType(ErrorType.Service); exception.setErrorCode("Service unavailable"); exception.setStatusCode(status); exception.setServiceName(request.getServiceName()); exception.fillInStackTrace();
@Override public AmazonServiceException handle(HttpResponse response) throws Exception { final AmazonServiceException ase = handleAse(response); ase.setStatusCode(response.getStatusCode()); ase.setServiceName(response.getRequest().getServiceName()); awsRequestMetrics.addPropertyWith(AWSRequestMetrics.Field.AWSRequestID, ase.getRequestId()) .addPropertyWith(AWSRequestMetrics.Field.AWSErrorCode, ase.getErrorCode()) .addPropertyWith(AWSRequestMetrics.Field.StatusCode, ase.getStatusCode()); return ase; }
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; } } }
@Override public AmazonServiceException handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); AmazonServiceException ase = new AmazonServiceException("Test"); ase.setErrorCode("TestError"); ase.setErrorType(ErrorType.Service); ase.setServiceName(request.getServiceName()); ase.setStatusCode(response.getStatusCode()); return ase; }
@Override public AmazonServiceException handle(HttpResponse response) throws Exception { JsonContent jsonContent = JsonContent.createJsonContent(response, jsonFactory); String errorCode = errorCodeParser.parseErrorCode(response, jsonContent); AmazonServiceException ase = createException(errorCode, jsonContent); // Jackson has special-casing for 'message' values when deserializing // Throwables, but sometimes the service passes the error message in // other JSON fields - handle it here. if (ase.getErrorMessage() == null) { ase.setErrorMessage(errorMessageParser.parseErrorMessage(response, jsonContent.getJsonNode())); } ase.setErrorCode(errorCode); ase.setServiceName(response.getRequest().getServiceName()); ase.setStatusCode(response.getStatusCode()); ase.setErrorType(getErrorTypeFromStatusCode(response.getStatusCode())); ase.setRawResponse(jsonContent.getRawContent()); String requestId = getRequestIdFromHeaders(response.getHeaders()); if (requestId != null) { ase.setRequestId(requestId); } ase.setHttpHeaders(response.getHeaders()); return ase; }
static AmazonServiceException amazonServiceException(Exception exception, String serviceName, int statusCode) { if (exception instanceof AmazonServiceException) { return (AmazonServiceException) exception; } final AmazonServiceException ase = new AmazonServiceException(exception.getMessage(), exception); ase.setStatusCode(statusCode); ase.setErrorCode(DEFAULT_UNKNOWN); ase.setServiceName(serviceName); ase.setErrorType(AmazonServiceException.ErrorType.Unknown); return ase; } }
@Test(expected = RuntimeException.class) public void testNonRetryableASEException() throws Exception { AmazonServiceException ase = new AmazonServiceException("Test"); ase.setErrorCode("ArbitNonRetryableException"); ase.setStatusCode(400); when(call.call()).thenThrow(ase); DynamoDBFibonacciRetryer retryer = new DynamoDBFibonacciRetryer(Duration.standardSeconds(10)); try { retryer.runWithRetry(call, null, null); } finally { verify(call).call(); } }
@Test(expected = AmazonServiceException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception_in_methodnotallow_code() { AmazonServiceException exception = new AmazonServiceException("method not allow exception"); exception.setStatusCode(HttpStatus.SC_METHOD_NOT_ALLOWED); exception.setErrorType(AmazonServiceException.ErrorType.Client); doThrow(exception).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }
@Override public AmazonServiceException handle(HttpResponse response) throws Exception { final AmazonServiceException ase = handleAse(response); ase.setStatusCode(response.getStatusCode()); ase.setServiceName(response.getRequest().getServiceName()); awsRequestMetrics.addPropertyWith(AWSRequestMetrics.Field.AWSRequestID, ase.getRequestId()) .addPropertyWith(AWSRequestMetrics.Field.AWSErrorCode, ase.getErrorCode()) .addPropertyWith(AWSRequestMetrics.Field.StatusCode, ase.getStatusCode()); return ase; }