/** * @see com.amazonaws.transform.Unmarshaller#unmarshall(java.lang.Object) */ public AmazonServiceException unmarshall(Node in) throws Exception { XPath xpath = xpath(); String errorCode = parseErrorCode(in, xpath); String errorType = asString("ErrorResponse/Error/Type", in, xpath); String requestId = asString("ErrorResponse/RequestId", in, xpath); String message = asString("ErrorResponse/Error/Message", in, xpath); AmazonServiceException ase = newException(message); ase.setErrorCode(errorCode); ase.setRequestId(requestId); if (errorType == null) { ase.setErrorType(ErrorType.Unknown); } else if (errorType.equalsIgnoreCase("Receiver")) { ase.setErrorType(ErrorType.Service); } else if (errorType.equalsIgnoreCase("Sender")) { ase.setErrorType(ErrorType.Client); } return ase; }
@Override public AmazonServiceException unmarshall(Node in) throws Exception { XPath xpath = xpath(); String errorCode = parseErrorCode(in, xpath); String message = asString("Response/Errors/Error/Message", in, xpath); String requestId = asString("Response/RequestID", in, xpath); String errorType = asString("Response/Errors/Error/Type", in, xpath); Constructor<? extends AmazonServiceException> constructor = exceptionClass.getConstructor(String.class); AmazonServiceException ase = constructor.newInstance(message); ase.setErrorCode(errorCode); ase.setRequestId(requestId); if (errorType == null) { ase.setErrorType(ErrorType.Unknown); } else if (errorType.equalsIgnoreCase("server")) { ase.setErrorType(ErrorType.Service); } else if (errorType.equalsIgnoreCase("client")) { ase.setErrorType(ErrorType.Client); } return ase; }
@Test public void handle_NullContent_ReturnsGenericAmazonServiceException() throws Exception { httpResponse.setStatusCode(500); httpResponse.setContent(null); AmazonServiceException ase = responseHandler.handle(httpResponse); // We assert these common properties are set again to make sure that code path is exercised // for unknown AmazonServiceExceptions as well assertEquals(ERROR_CODE, ase.getErrorCode()); assertEquals(500, ase.getStatusCode()); assertEquals(SERVICE_NAME, ase.getServiceName()); assertEquals(ErrorType.Service, ase.getErrorType()); }
@Test public void handle_UnmarshallerReturnsException_ClientErrorType() throws Exception { httpResponse.setStatusCode(400); expectUnmarshallerMatches(); when(unmarshaller.unmarshall((JsonNode) anyObject())) .thenReturn(new CustomException("error")); AmazonServiceException ase = responseHandler.handle(httpResponse); assertEquals(ERROR_CODE, ase.getErrorCode()); assertEquals(400, ase.getStatusCode()); assertEquals(SERVICE_NAME, ase.getServiceName()); assertEquals(ErrorType.Client, ase.getErrorType()); }
@Test public void handle_UnmarshallerReturnsException_ServiceErrorType() throws Exception { httpResponse.setStatusCode(500); expectUnmarshallerMatches(); when(unmarshaller.unmarshall((JsonNode) anyObject())) .thenReturn(new CustomException("error")); AmazonServiceException ase = responseHandler.handle(httpResponse); assertEquals(ErrorType.Service, ase.getErrorType()); }
@Override public ObjectMetadata getObjectMetadata(String bucketName, String key) throws AmazonServiceException { AmazonS3Exception exception = new AmazonS3Exception("Internal Error"); exception.setStatusCode(500); exception.setErrorType(ErrorType.Service); throw exception; }
@Override public ObjectMetadata getObjectMetadata(String bucketName, String key) throws AmazonServiceException { AmazonS3Exception exception = new AmazonS3Exception("Not Found"); exception.setStatusCode(404); exception.setErrorType(ErrorType.Client); throw exception; }
@Override public ObjectMetadata getObjectMetadata(String bucketName, String key) throws AmazonServiceException { AmazonS3Exception exception = new AmazonS3Exception("Bad Auth"); exception.setStatusCode(403); exception.setErrorType(ErrorType.Client); throw exception; }
@Override public ObjectMetadata getObjectMetadata(String bucketName, String key) throws AmazonServiceException { AmazonS3Exception exception = new AmazonS3Exception("Not authed"); exception.setStatusCode(403); exception.setErrorType(ErrorType.Client); throw exception; }
private BucketInfo getBucketInfo(final String name) { final BucketInfo info = getBucketInfoOrNull(name); if (info == null) { final AmazonServiceException e = new AmazonServiceException("The specified bucket does not exist"); e.setStatusCode(404); e.setErrorType(ErrorType.Client); e.setServiceName("Amazon S3"); e.setErrorCode("NoSuchBucket"); throw e; } return info; }
private S3ObjectInfo getObject(final String key) { final S3ObjectInfo info = getObjectOrNull(key); if (info == null) { final AmazonServiceException e = new AmazonServiceException("The specified key does not exist"); e.setErrorCode("NoSuchKey"); e.setErrorType(ErrorType.Client); e.setServiceName("Amazon S3"); e.setStatusCode(404); throw e; } return info; }
@Test public void listObjectsFailsIfNoBucket() { try { s3.listObjects("b"); } catch (final AmazonServiceException e) { assertThat(e.getStatusCode(), is(404)); assertThat(e.getErrorCode(), is("NoSuchBucket")); assertThat(e.getErrorType(), is(ErrorType.Client)); } }
@Test public void getObjectWithInvalidBucket() { try { s3.getObject("b", "a"); } catch (final AmazonServiceException e) { assertThat(e.getStatusCode(), is(404)); assertThat(e.getErrorCode(), is("NoSuchBucket")); assertThat(e.getErrorType(), is(ErrorType.Client)); } }
@Test public void getObjectWithInvalidKey() { s3.createBucket("b"); try { s3.getObject("b", "a"); } catch (final AmazonServiceException e) { assertThat(e.getStatusCode(), is(404)); assertThat(e.getErrorCode(), is("NoSuchKey")); assertThat(e.getErrorType(), is(ErrorType.Client)); } }
public AmazonServiceException handle(HttpResponse response) throws Exception { JSONObject jsonBody = getBodyAsJson(response); Class<? extends AmazonServiceException> exceptionClass = exceptionClasses.get(response.getStatusCode()); AmazonServiceException result; // Support other attribute names for the message? // TODO: Inspect exception type (caching details) and apply other values from the body String message = jsonBody.has("message") ? jsonBody.getString("message") : jsonBody.getString("Message"); if (exceptionClass != null) { result = exceptionClass.getConstructor(String.class).newInstance(message); } else { result = AmazonServiceException.class.getConstructor(String.class).newInstance(message); } result.setServiceName(response.getRequest().getServiceName()); result.setStatusCode(response.getStatusCode()); if (response.getStatusCode() < 500) { result.setErrorType(ErrorType.Client); } else { result.setErrorType(ErrorType.Service); } for (Entry<String, String> headerEntry : response.getHeaders().entrySet()) { if (headerEntry.getKey().equalsIgnoreCase("X-Amzn-RequestId")) { result.setRequestId(headerEntry.getValue()); } } return result; }
private ErrorType getErrorTypeFromStatusCode(int statusCode) { return statusCode < 500 ? ErrorType.Client : ErrorType.Service; }
@Test public void shouldHandleServiceErrorForGetShardIterator() { shouldHandleGetShardIteratorError(newAmazonServiceException(ErrorType.Service), TransientKinesisException.class); }
@Test public void shouldHandleClientErrorForGetShardIterator() { shouldHandleGetShardIteratorError(newAmazonServiceException(ErrorType.Client), RuntimeException.class); }
@Test public void shouldHandleServiceErrorForShardListing() { shouldHandleShardListingError(newAmazonServiceException(ErrorType.Service), TransientKinesisException.class); }
@Test public void shouldHandleClientErrorForShardListing() { shouldHandleShardListingError(newAmazonServiceException(ErrorType.Client), RuntimeException.class); }
@Test public void shouldHandleServiceErrorForGetBacklogBytes() { shouldHandleGetBacklogBytesError(newAmazonServiceException(ErrorType.Service), TransientKinesisException.class); }
@Test public void shouldHandleClientErrorForGetBacklogBytes() { shouldHandleGetBacklogBytesError(newAmazonServiceException(ErrorType.Client), RuntimeException.class); }
private AmazonServiceException newAmazonServiceException(ErrorType errorType) { AmazonServiceException exception = new AmazonServiceException(""); exception.setErrorType(errorType); return exception; }
@Test public void testIsRecoverableExceptionWithProvisionedThroughputExceeded() { final ProvisionedThroughputExceededException ex = new ProvisionedThroughputExceededException("asdf"); ex.setErrorType(ErrorType.Client); assertTrue(KinesisProxy.isRecoverableException(ex)); }
@Test public void testIsRecoverableExceptionWithServiceException() { final AmazonServiceException ex = new AmazonServiceException("asdf"); ex.setErrorType(ErrorType.Service); assertTrue(KinesisProxy.isRecoverableException(ex)); }
@Test public void testIsRecoverableExceptionWithExpiredIteratorException() { final ExpiredIteratorException ex = new ExpiredIteratorException("asdf"); ex.setErrorType(ErrorType.Client); assertFalse(KinesisProxy.isRecoverableException(ex)); }
public S3ThrowableAssert hasErrorType(ErrorType errorType) { Assertions.assertThat(actual.getErrorType()).isEqualTo(errorType); return myself; }
/** * Returns the AWS error type information by looking at the HTTP status code * in the error response. S3 error responses don't explicitly declare a * sender or client fault like other AWS services, so we have to use the * HTTP status code to infer this information. * * @param httpResponse * The HTTP error response to use to determine the right error * type to set. */ private ErrorType errorTypeOf(int statusCode) { return statusCode >= 500 ? ErrorType.Service : ErrorType.Client; }