Example usage for com.amazonaws AmazonServiceException setErrorCode

List of usage examples for com.amazonaws AmazonServiceException setErrorCode

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException setErrorCode.

Prototype

public void setErrorCode(String errorCode) 

Source Link

Document

Sets the AWS error code represented by this exception.

Usage

From source file:com.msi.dns53.client.DNS53Client.java

License:Apache License

@SuppressWarnings("unchecked")
public void exceptionMapper(ClientResponse response, String resultXml) throws AmazonServiceException {
    ErrorResponsePOJO er = null;//from   ww w  .j a  v a  2  s  . co m
    try {
        StringReader reader = new StringReader(resultXml);
        JAXBContext context = JAXBContext.newInstance(ErrorResponsePOJO.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        er = (ErrorResponsePOJO) unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
        throw new AmazonClientException("There was a problem parsing the error response xml with JAXB.", e);
    }
    if (er == null || er.getError() == null || er.getRequestId() == null || er.getError().getCode() == null
            || er.getError().getMessage() == null || er.getError().getType() == null) {
        throw new AmazonClientException(
                "Error response xml did not contain expected elements although it is well formed.");
    }

    String errCode = er.getError().getCode();
    Class<AmazonServiceException> clazz = null;
    Constructor<AmazonServiceException> c = null;
    AmazonServiceException exception = null;
    try {
        String clazzName = ExceptionMap.getExceptionMap().getMap().get(errCode);
        clazz = (Class<AmazonServiceException>) Class.forName(clazzName);
        c = (Constructor<AmazonServiceException>) clazz.getConstructor(String.class);
        exception = (AmazonServiceException) c.newInstance(new Object[] { er.getError().getMessage() });
    } catch (NullPointerException e) {
        exception = new AmazonServiceException(er.getError().getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        throw new AmazonClientException("Client could not determine the type of the error response.");
    }
    if (exception == null) {
        throw new AmazonClientException(
                "Client encountered a problem while it was mapping the error response.");
    }
    exception.setErrorCode(er.getError().getCode());
    ErrorType et = ErrorType.Unknown;
    if ("Sender".equals(er.getError().getType())) {
        et = ErrorType.Service;
    }
    exception.setErrorType(et);
    exception.setRequestId(er.getRequestId());
    exception.setStatusCode(response.getStatus());
    exception.setServiceName("DNS53");
    throw exception;
}

From source file:com.netflix.edda.AwsException.java

License:Apache License

public static void raise(int code, String svc, String reqId, String error, String msg) {
    StringBuffer buf = new StringBuffer().append("Status Code: ").append(code).append(", AWS Service: ")
            .append(svc).append(", AWS Request ID: ").append(reqId).append(", AWS Error Code: ").append(error)
            .append(", AWS Error Message:").append(msg);
    AmazonServiceException e = new AmazonServiceException(buf.toString());
    e.setStatusCode(code);/*w w w . ja  v  a  2s  .  c o  m*/
    e.setServiceName(svc);
    e.setRequestId(reqId);
    e.setErrorCode(error);
    throw e;
}

From source file:com.netflix.edda.EddaAwsClient.java

License:Apache License

protected byte[] doGet(final String uri) {
    try {// w w  w . j  ava  2 s  .c o m
        return EddaContext.getContext().getRxHttp().get(mkUrl(uri)).flatMap(response -> {
            if (response.getStatus().code() != 200) {
                AmazonServiceException e = new AmazonServiceException("Failed to fetch " + uri);
                e.setStatusCode(response.getStatus().code());
                e.setErrorCode("Edda");
                e.setRequestId(uri);
                return rx.Observable.error(e);
            }
            return response.getContent().reduce(new ByteArrayOutputStream(), (out, bb) -> {
                try {
                    bb.readBytes(out, bb.readableBytes());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                return out;
            }).map(out -> {
                return out.toByteArray();
            });
        }).toBlocking().toFuture().get(2, TimeUnit.MINUTES);
    } catch (Exception e) {
        throw new RuntimeException("failed to get url: " + uri, e);
    }
}

From source file:com.netflix.spinnaker.kork.aws.AwsMetricsSupport.java

License:Apache License

static AmazonServiceException amazonServiceException(Exception exception, String serviceName, int statusCode) {
    if (exception instanceof AmazonServiceException) {
        return (AmazonServiceException) exception;
    }/*www  .  j a v  a  2s  .  c om*/

    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;
}

From source file:org.finra.dm.dao.impl.MockEc2OperationsImpl.java

License:Apache License

@Override
public void modifyInstanceAttribute(AmazonEC2Client ec2Client,
        ModifyInstanceAttributeRequest modifyInstanceAttributeRequest) {
    if (modifyInstanceAttributeRequest.getGroups() != null && modifyInstanceAttributeRequest.getGroups().get(0)
            .equals(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION)) {
        throw new AmazonServiceException(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION);
    }//from  w ww . j  av  a  2  s . c o m

    if (modifyInstanceAttributeRequest.getGroups() != null && modifyInstanceAttributeRequest.getGroups().get(0)
            .equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");

        throw throttlingException;
    }

}

From source file:org.finra.dm.dao.impl.MockEc2OperationsImpl.java

License:Apache License

/**
 * In-memory implementation of describeSubnets. Returns the subnets from the pre-configured subnets.
 * The method can be ordered to throw an AmazonServiceException with a specified error code by specifying a subnet ID with prefix "throw." followed by a
 * string indicating the error code to throw. The exception thrown in this manner will always set the status code to 500.
 *///w w  w  . ja v a 2 s  .  c o m
@Override
public DescribeSubnetsResult describeSubnets(AmazonEC2Client ec2Client,
        DescribeSubnetsRequest describeSubnetsRequest) {
    List<Subnet> subnets = new ArrayList<>();

    List<String> requestedSubnetIds = describeSubnetsRequest.getSubnetIds();

    // add all subnets if request is empty (this is AWS behavior)
    if (requestedSubnetIds.isEmpty()) {
        requestedSubnetIds.addAll(mockSubnets.keySet());
    }

    for (String requestedSubnetId : requestedSubnetIds) {
        MockSubnet mockSubnet = mockSubnets.get(requestedSubnetId);

        // Throw exception if any of the subnet ID do not exist
        if (mockSubnet == null) {
            AmazonServiceException amazonServiceException;

            if (requestedSubnetId.startsWith("throw.")) {
                String errorCode = requestedSubnetId.substring("throw.".length());

                amazonServiceException = new AmazonServiceException(errorCode);
                amazonServiceException.setErrorCode(errorCode);
                amazonServiceException.setStatusCode(500);
            } else {
                amazonServiceException = new AmazonServiceException(
                        "The subnet ID '" + requestedSubnetId + "' does not exist");
                amazonServiceException.setErrorCode(Ec2DaoImpl.ERROR_CODE_SUBNET_ID_NOT_FOUND);
                amazonServiceException.setStatusCode(400);
            }

            throw amazonServiceException;
        }

        subnets.add(mockSubnet.toAwsObject());
    }

    DescribeSubnetsResult describeSubnetsResult = new DescribeSubnetsResult();
    describeSubnetsResult.setSubnets(subnets);
    return describeSubnetsResult;
}

From source file:org.finra.dm.dao.impl.MockEmrOperationsImpl.java

License:Apache License

@Override
public String runEmrJobFlow(AmazonElasticMapReduceClient emrClient, RunJobFlowRequest jobFlowRequest) {
    String clusterStatus = ClusterState.BOOTSTRAPPING.toString();

    if (StringUtils.isNotBlank(jobFlowRequest.getAmiVersion())) {
        if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
            AmazonServiceException throttlingException = new AmazonServiceException(
                    "test throttling exception");
            throttlingException.setErrorCode("ThrottlingException");

            throw throttlingException;
        } else if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_BAD_REQUEST)) {
            AmazonServiceException badRequestException = new AmazonServiceException(
                    MockAwsOperationsHelper.AMAZON_BAD_REQUEST);
            badRequestException.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            throw badRequestException;
        } else if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_NOT_FOUND)) {
            AmazonServiceException notFoundException = new AmazonServiceException(
                    MockAwsOperationsHelper.AMAZON_NOT_FOUND);
            notFoundException.setStatusCode(HttpStatus.SC_NOT_FOUND);
            throw notFoundException;
        } else if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION)) {
            throw new AmazonServiceException(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION);
        } else if (jobFlowRequest.getAmiVersion()
                .equals(MockAwsOperationsHelper.AMAZON_CLUSTER_STATUS_WAITING)) {
            clusterStatus = ClusterState.WAITING.toString();
        } else if (jobFlowRequest.getAmiVersion()
                .equals(MockAwsOperationsHelper.AMAZON_CLUSTER_STATUS_RUNNING)) {
            clusterStatus = ClusterState.RUNNING.toString();
        }/*from ww w  .  j  a  v  a 2  s. c  o  m*/
    }

    return createNewCluster(jobFlowRequest, clusterStatus).getJobFlowId();
}

From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java

License:Apache License

/**
 * <p>/* www .  ja v a2 s  .com*/
 * Creates and returns a new {@link ObjectMetadata} with the given parameters. Content length is defaulted to 1 bytes unless a hint is provided.
 * </p>
 * <p>
 * Takes the following hints when filePath is suffixed:
 * </p>
 * <dl>
 * <p/>
 * <dt>AMAZON_THROTTLING_EXCEPTION</dt>
 * <dd>Throws AmazonServiceException with the error code "ThrottlingException"</dd>
 * <p/>
 * <dt>MOCK_S3_FILE_NAME_SERVICE_EXCEPTION</dt>
 * <dd>Throws AmazonServiceException</dd>
 * <p/>
 * <dt>MOCK_S3_FILE_NAME_NOT_FOUND</dt>
 * <dd>Throws AmazonServiceException with status code SC_NOT_FOUND</dd>
 * <p/>
 * <dt>MOCK_S3_FILE_NAME_0_BYTE_SIZE</dt>
 * <dd>Sets content length to 0 bytes</dd>
 * <p/>
 * </dl>
 */
@Override
public ObjectMetadata getObjectMetadata(String sourceBucketName, String filePath, AmazonS3Client s3Client) {
    ObjectMetadata objectMetadata = new ObjectMetadata();

    if (filePath.endsWith(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");

        throw throttlingException;
    } else if (filePath.endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) {
        throw new AmazonServiceException(null);
    } else if (filePath.endsWith(MOCK_S3_FILE_NAME_NOT_FOUND)) {
        AmazonServiceException exception = new AmazonServiceException(null);
        exception.setStatusCode(HttpStatus.SC_NOT_FOUND);
        throw exception;
    } else if (filePath.endsWith(MOCK_S3_FILE_NAME_0_BYTE_SIZE)) {
        objectMetadata.setContentLength(AbstractCoreTest.FILE_SIZE_0_BYTE);
    } else {
        objectMetadata.setContentLength(AbstractCoreTest.FILE_SIZE_1_KB);
    }

    return objectMetadata;
}

From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java

License:Apache License

@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3) {
    String bucketName = getObjectRequest.getBucketName();
    String key = getObjectRequest.getKey();

    if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(bucketName)) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_NO_SUCH_BUCKET);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_BUCKET);
        throw amazonServiceException;
    }//ww  w.  j  a  v a 2s  .c  o  m

    if (MOCK_S3_BUCKET_NAME_ACCESS_DENIED.equals(bucketName)) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_ACCESS_DENIED);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_ACCESS_DENIED);
        throw amazonServiceException;
    }

    if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(bucketName)) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_INTERNAL_ERROR);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_INTERNAL_ERROR);
        throw amazonServiceException;
    }

    MockS3Bucket mockS3Bucket = getOrCreateBucket(bucketName);
    MockS3Object mockS3Object = mockS3Bucket.getObjects().get(key);

    if (mockS3Object == null) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_NO_SUCH_KEY);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_KEY);
        throw amazonServiceException;
    }

    S3Object s3Object = new S3Object();
    s3Object.setBucketName(bucketName);
    s3Object.setKey(key);
    s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData()));
    s3Object.setObjectMetadata(mockS3Object.getObjectMetadata());
    return s3Object;
}

From source file:org.finra.dm.dao.impl.MockSqsOperationsImpl.java

License:Apache License

@Override
public void sendSqsTextMessage(ClientConfiguration client, String queueName, String messageText) {
    // Throw a throttling exception for a specific queue name for testing purposes.
    if (queueName.equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");
        throw throttlingException;
    }/*from w  ww.j  a  va 2s.  c  o m*/

    // Throw an illegal state exception for a specific queue name for testing purposes.
    if (queueName.equals(MOCK_SQS_QUEUE_NOT_FOUND_NAME)) {
        throw new IllegalStateException(String.format("AWS SQS queue with \"%s\" name not found.", queueName));
    }

    // Nothing else to do in the normal case since our unit tests aren't reading messages once they have been published.
}