@Override public boolean matches(AmazonServiceException ase) { return expectedStatusCode == ase.getStatusCode(); }
private int extractHttpStatusCode(AmazonServiceException exception) { return exception.getStatusCode(); } }
@Override public boolean doesBucketExist(String bucketName) throws SdkClientException, AmazonServiceException { try { headBucket(new HeadBucketRequest(bucketName)); return true; } catch (AmazonServiceException ase) { // A redirect error or a forbidden error means the bucket exists. So // returning true. if ((ase.getStatusCode() == Constants.BUCKET_REDIRECT_STATUS_CODE) || (ase.getStatusCode() == Constants.BUCKET_ACCESS_FORBIDDEN_STATUS_CODE)) { return true; } if (ase.getStatusCode() == Constants.NO_SUCH_BUCKET_STATUS_CODE) { return false; } throw ase; } }
@Override public boolean doesBucketExistV2(String bucketName) throws SdkClientException { try { getBucketAcl(bucketName); return true; } catch (AmazonServiceException ase) { // A redirect error or an AccessDenied exception means the bucket exists but it's not in this region // or we don't have permissions to it. if ((ase.getStatusCode() == Constants.BUCKET_REDIRECT_STATUS_CODE) || "AccessDenied".equals(ase.getErrorCode())) { return true; } if (ase.getStatusCode() == Constants.NO_SUCH_BUCKET_STATUS_CODE) { return false; } throw ase; } }
/** * Returns true if the specified exception is a request entity too large error. * * @param exception The exception to test. * @return True if the exception resulted from a request entity too large error message from a service, otherwise false. */ public static boolean isRequestEntityTooLargeException(SdkBaseException exception) { return isAse(exception) && toAse(exception).getStatusCode() == HttpStatus.SC_REQUEST_TOO_LONG; }
/** * Returns true if the specified exception is a retryable service side exception. * * @param exception The exception to test. * @return True if the exception resulted from a retryable service error, otherwise false. */ public static boolean isRetryableServiceException(SdkBaseException exception) { if (!isAse(exception)) { return false; } AmazonServiceException ase = toAse(exception); return RETRYABLE_STATUS_CODES.contains(ase.getStatusCode()) || RETRYABLE_ERROR_CODES.contains(ase.getErrorCode()); }
/** * Returns true if the specified exception is a throttling error. * * @param exception The exception to test. * @return True if the exception resulted from a throttling error message from a service, otherwise false. */ public static boolean isThrottlingException(SdkBaseException exception) { if (!isAse(exception)) { return false; } final AmazonServiceException ase = toAse(exception); return THROTTLING_ERROR_CODES.contains(ase.getErrorCode()) || ase.getStatusCode() == 429; }
@Override @Nullable protected ObjectStatus getObjectStatus(String key) throws IOException { try { ObjectMetadata meta = mClient.getObjectMetadata(mBucketName, key); return new ObjectStatus(key, meta.getETag(), meta.getContentLength(), meta.getLastModified().getTime()); } catch (AmazonServiceException e) { if (e.getStatusCode() == 404) { // file not found, possible for exists calls return null; } throw new IOException(e); } catch (AmazonClientException e) { throw new IOException(e); } }
protected List<FlowFile> processServiceException(final ProcessSession session, List<FlowFile> flowFiles, AmazonServiceException exception) { List<FlowFile> failedFlowFiles = new ArrayList<>(); for (FlowFile flowFile : flowFiles) { Map<String,String> attributes = new HashMap<>(); attributes.put(DYNAMODB_ERROR_EXCEPTION_MESSAGE, exception.getMessage() ); attributes.put(DYNAMODB_ERROR_CODE, exception.getErrorCode() ); attributes.put(DYNAMODB_ERROR_MESSAGE, exception.getErrorMessage() ); attributes.put(DYNAMODB_ERROR_TYPE, exception.getErrorType().name() ); attributes.put(DYNAMODB_ERROR_SERVICE, exception.getServiceName() ); attributes.put(DYNAMODB_ERROR_RETRYABLE, Boolean.toString(exception.isRetryable())); attributes.put(DYNAMODB_ERROR_REQUEST_ID, exception.getRequestId() ); attributes.put(DYNAMODB_ERROR_STATUS_CODE, Integer.toString(exception.getStatusCode()) ); attributes.put(DYNAMODB_ERROR_EXCEPTION_MESSAGE, exception.getMessage() ); attributes.put(DYNAMODB_ERROR_RETRYABLE, Boolean.toString(exception.isRetryable())); flowFile = session.putAllAttributes(flowFile, attributes); failedFlowFiles.add(flowFile); } return failedFlowFiles; }
/** * Populate exception attributes in the flow file * @param session process session * @param flowFile the flow file * @param exception exception thrown during invocation * @return FlowFile the updated flow file */ private FlowFile populateExceptionAttributes(final ProcessSession session, FlowFile flowFile, final AmazonServiceException exception) { Map<String,String> attributes = new HashMap<>(); attributes.put(AWS_LAMBDA_EXCEPTION_MESSAGE, exception.getErrorMessage()); attributes.put(AWS_LAMBDA_EXCEPTION_ERROR_CODE, exception.getErrorCode()); attributes.put(AWS_LAMBDA_EXCEPTION_REQUEST_ID, exception.getRequestId()); attributes.put(AWS_LAMBDA_EXCEPTION_STATUS_CODE, Integer.toString(exception.getStatusCode())); if ( exception.getCause() != null ) attributes.put(AWS_LAMBDA_EXCEPTION_CAUSE, exception.getCause().getMessage()); attributes.put(AWS_LAMBDA_EXCEPTION_ERROR_TYPE, exception.getErrorType().toString()); attributes.put(AWS_LAMBDA_EXCEPTION_MESSAGE, exception.getErrorMessage()); flowFile = session.putAllAttributes(flowFile, attributes); return flowFile; }
/** * Returns {@code true} if mapping presents for the provided key. * * @param key Key to check mapping for. * @return {@code true} if mapping presents for key. * @throws AmazonClientException If an error occurs while querying Amazon S3. */ boolean hasKey(String key) throws AmazonClientException { assert !F.isEmpty(key); try { return s3.getObjectMetadata(bucketName, key).getContentLength() != 0; } catch (AmazonServiceException e) { if (e.getStatusCode() != 404) throw e; } return false; }
@Override public String getMessage() { return getErrorMessage() + " (Service: " + getServiceName() + "; Status Code: " + getStatusCode() + "; Error Code: " + getErrorCode() + "; Request ID: " + getRequestId() + ")"; }
public static void main(String[] args) { AmazonGuardDuty guardduty = AmazonGuardDutyClientBuilder.defaultClient(); try { ListDetectorsRequest request = new ListDetectorsRequest(); ListDetectorsResult response = guardduty.listDetectors(request); for (String detectorId : response.getDetectorIds()) { System.out.println("DetectorId: " + detectorId ); } } catch (AmazonServiceException ase) { System.out.println("Caught Exception: " + ase.getMessage()); System.out.println("Reponse Status Code: " + ase.getStatusCode()); System.out.println("Error Code: " + ase.getErrorCode()); System.out.println("Request ID: " + ase.getRequestId()); } } }
if (e.getStatusCode() != 404) throw e;
@Override public BucketCrossOriginConfiguration getBucketCrossOriginConfiguration(GetBucketCrossOriginConfigurationRequest getBucketCrossOriginConfigurationRequest) { getBucketCrossOriginConfigurationRequest = beforeClientExecution(getBucketCrossOriginConfigurationRequest); rejectNull(getBucketCrossOriginConfigurationRequest, "The request object parameter getBucketCrossOriginConfigurationRequest must be specified."); String bucketName = getBucketCrossOriginConfigurationRequest.getBucketName(); rejectNull(bucketName, "The bucket name must be specified when retrieving the bucket cross origin configuration."); Request<GetBucketCrossOriginConfigurationRequest> request = createRequest(bucketName, null, getBucketCrossOriginConfigurationRequest, HttpMethodName.GET); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "GetBucketCors"); request.addParameter("cors", null); try { return invoke(request, new Unmarshallers.BucketCrossOriginConfigurationUnmarshaller(), bucketName, null); } catch (AmazonServiceException ase) { switch (ase.getStatusCode()) { case 404: return null; default: throw ase; } } }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest) { getBucketLifecycleConfigurationRequest = beforeClientExecution(getBucketLifecycleConfigurationRequest); rejectNull(getBucketLifecycleConfigurationRequest, "The request object pamameter getBucketLifecycleConfigurationRequest must be specified."); String bucketName = getBucketLifecycleConfigurationRequest.getBucketName(); rejectNull(bucketName, "The bucket name must be specifed when retrieving the bucket lifecycle configuration."); Request<GetBucketLifecycleConfigurationRequest> request = createRequest(bucketName, null, getBucketLifecycleConfigurationRequest, HttpMethodName.GET); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "GetBucketLifecycleConfiguration"); request.addParameter("lifecycle", null); try { return invoke(request, new Unmarshallers.BucketLifecycleConfigurationUnmarshaller(), bucketName, null); } catch (AmazonServiceException ase) { switch (ase.getStatusCode()) { case 404: return null; default: throw ase; } } }
@Override public BucketTaggingConfiguration getBucketTaggingConfiguration(GetBucketTaggingConfigurationRequest getBucketTaggingConfigurationRequest) { getBucketTaggingConfigurationRequest = beforeClientExecution(getBucketTaggingConfigurationRequest); rejectNull(getBucketTaggingConfigurationRequest, "The request object parameter getBucketTaggingConfigurationRequest must be specifed."); String bucketName = getBucketTaggingConfigurationRequest.getBucketName(); rejectNull(bucketName, "The bucket name must be specified when retrieving the bucket tagging configuration."); Request<GetBucketTaggingConfigurationRequest> request = createRequest(bucketName, null, getBucketTaggingConfigurationRequest, HttpMethodName.GET); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "GetBucketTagging"); request.addParameter("tagging", null); try { return invoke(request, new Unmarshallers.BucketTaggingConfigurationUnmarshaller(), bucketName, null); } catch (AmazonServiceException ase) { switch (ase.getStatusCode()) { case 404: return null; default: throw ase; } } }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest) throws SdkClientException, AmazonServiceException { getBucketWebsiteConfigurationRequest = beforeClientExecution(getBucketWebsiteConfigurationRequest); rejectNull(getBucketWebsiteConfigurationRequest, "The request object parameter getBucketWebsiteConfigurationRequest must be specified."); String bucketName = getBucketWebsiteConfigurationRequest.getBucketName(); rejectNull(bucketName, "The bucket name parameter must be specified when requesting a bucket's website configuration"); Request<GetBucketWebsiteConfigurationRequest> request = createRequest(bucketName, null, getBucketWebsiteConfigurationRequest, HttpMethodName.GET); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "GetBucketWebsite"); request.addParameter("website", null); request.addHeader("Content-Type", "application/xml"); try { return invoke(request, new Unmarshallers.BucketWebsiteConfigurationUnmarshaller(), bucketName, null); } catch (AmazonServiceException ase) { if (ase.getStatusCode() == 404) return null; throw ase; } }
throw new AmazonClientException("Unrecognized service response for the dry-run request."); } catch (AmazonServiceException ase) { if (ase.getErrorCode().equals("DryRunOperation") && ase.getStatusCode() == 412) { return new DryRunResult<X>(true, request, ase.getMessage(), ase); } else if (ase.getErrorCode().equals("UnauthorizedOperation") && ase.getStatusCode() == 403) { return new DryRunResult<X>(false, request, ase.getMessage(), ase); } else if (ase.getErrorCode().equals("AuthFailure")) {
@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; }