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; }
System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) {
System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) {
@Test public void testUnmarshall() throws TransformerFactoryConfigurationError, Exception { Document xmlDoc = new DocumentImpl(); Element root = xmlDoc.createElement("ErrorResponse"); Element requestId = xmlDoc.createElement("RequestId"); requestId.appendChild(xmlDoc.createTextNode("TestId")); Element error = xmlDoc.createElement("Error"); Element message = xmlDoc.createElement("Message"); message.appendChild(xmlDoc.createTextNode("TestMessage")); Element type = xmlDoc.createElement("Type"); type.appendChild(xmlDoc.createTextNode("Receiver")); error.appendChild(message); error.appendChild(type); root.appendChild(error); root.appendChild(requestId); xmlDoc.appendChild(root); StandardErrorUnmarshaller seu = new StandardErrorUnmarshaller(); AmazonServiceException ase = seu.unmarshall(xmlDoc); assertEquals(ase.getErrorMessage(), "TestMessage"); assertEquals(ase.getErrorType(), ErrorType.Service); assertEquals(ase.getRequestId(), "TestId"); Element code = xmlDoc.createElement("Code"); code.appendChild(xmlDoc.createTextNode("TestCode")); }
@Test public void testUnmarshall() throws TransformerFactoryConfigurationError, Exception { Document xmlDoc = new DocumentImpl(); Element root = xmlDoc.createElement("Response"); Element requestId = xmlDoc.createElement("RequestID"); requestId.appendChild(xmlDoc.createTextNode("TestId")); Element errors = xmlDoc.createElement("Errors"); Element error = xmlDoc.createElement("Error"); Element message = xmlDoc.createElement("Message"); message.appendChild(xmlDoc.createTextNode("TestMessage")); Element type = xmlDoc.createElement("Type"); type.appendChild(xmlDoc.createTextNode("server")); Element code = xmlDoc.createElement("Code"); code.appendChild(xmlDoc.createTextNode("TestCode")); error.appendChild(message); error.appendChild(type); error.appendChild(code); errors.appendChild(error); root.appendChild(errors); root.appendChild(requestId); xmlDoc.appendChild(root); LegacyErrorUnmarshaller leu = new LegacyErrorUnmarshaller(); AmazonServiceException ase = leu.unmarshall(xmlDoc); assertEquals(ase.getErrorCode(), "TestCode"); assertEquals(ase.getErrorMessage(), "TestMessage"); assertEquals(ase.getErrorType(), ErrorType.Service); assertEquals(ase.getRequestId(), "TestId"); }
@Test public void testJsonErrorResponseReturnsServiceErrorTypeIfErrorStatus5XX() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getErrorType(), ErrorType.Service); }
private void printAmazonServiceException(AmazonServiceException ase) { LOG.info("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); LOG.info("Error Message: " + ase.getMessage()); LOG.info("HTTP Status Code: " + ase.getStatusCode()); LOG.info("AWS Error Code: " + ase.getErrorCode()); LOG.info("Error Type: " + ase.getErrorType()); LOG.info("Request ID: " + ase.getRequestId()); LOG.info("Class Name: " + ase.getClass().getName()); }
private void logAmazonServiceException( final AmazonServiceException exception, final AmazonWebServiceRequest request) { log.info( "Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); log.info("Error Message: {}", exception.getMessage()); log.info("HTTP Status Code: {}", exception.getStatusCode()); log.info("AWS Error Code: {}", exception.getErrorCode()); log.info("Error Type: {}", exception.getErrorType()); log.info("Request ID: {}", exception.getRequestId()); }
protected AmazonServiceException amazonServiceException(AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); return ase; }
@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); }
private void printAmazonServiceException(AmazonServiceException ase) { LOG.info("Caught an AmazonServiceException {}", ase.toString()); LOG.info("Error Message: {}", ase.getMessage()); LOG.info("HTTP Status Code: {}", ase.getStatusCode()); LOG.info("AWS Error Code: {}", ase.getErrorCode()); LOG.info("Error Type: {}", ase.getErrorType()); LOG.info("Request ID: {}", ase.getRequestId()); LOG.info("Stack", ase); } }
static String[] buildExceptionTags(AmazonWebServiceRequest originalRequest, Exception exception) { final AmazonServiceException ase = amazonServiceException(exception); String targetAccountId = DEFAULT_UNKNOWN; if (ase.getHttpHeaders() != null) { targetAccountId = ase.getHttpHeaders().get("targetAccountId"); } return new String[] { "requestType", originalRequest.getClass().getSimpleName(), "statusCode", Integer.toString(ase.getStatusCode()), "errorCode", Optional.ofNullable(ase.getErrorCode()).orElse(DEFAULT_UNKNOWN), "serviceName", Optional.ofNullable(ase.getServiceName()).orElse(DEFAULT_UNKNOWN), "errorType", Optional.ofNullable(ase.getErrorType()).orElse(AmazonServiceException.ErrorType.Unknown).name(), "accountId", Optional.ofNullable(targetAccountId).orElse(DEFAULT_UNKNOWN) }; }
@Test public void testHandleErrorResponseHandlerFailsWith413() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(413).build(); HttpResponseHandler<AmazonServiceException> errorResponseHandler = new HttpResponseHandler<AmazonServiceException>() { @Override public AmazonServiceException handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new Exception("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; AmazonServiceException e = client.handleErrorResponse(request, errorResponseHandler, httpResponse); assertEquals(e.getStatusCode(), 413); assertEquals(e.getErrorCode(), "Request entity too large"); assertEquals(e.getErrorType(), ErrorType.Client); assertEquals(e.getServiceName(), "ServiceName"); }
@Test public void testHandleErrorResponseHandlerFailsWith503() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder() .statusText("Service unavailable") .statusCode(503).build(); HttpResponseHandler<AmazonServiceException> errorResponseHandler = new HttpResponseHandler<AmazonServiceException>() { @Override public AmazonServiceException handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new Exception("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; AmazonServiceException e = client.handleErrorResponse(request, errorResponseHandler, httpResponse); assertEquals(e.getStatusCode(), 503); assertEquals(e.getErrorCode(), "Service unavailable"); assertEquals(e.getErrorType(), ErrorType.Service); assertEquals(e.getServiceName(), "ServiceName"); }
private static void logException(AmazonServiceException ase) { logger.error("AmazonServiceException: error={}, statuscode={}, awserrcode={}, errtype={}, reqid={}", ase.toString(), ase.getStatusCode(), ase.getErrorCode(), ase.getErrorType(), ase.getRequestId()); }
private void logException(AmazonServiceException ase) { logger.error("AmazonServiceException: error={}, statuscode={}, " + "awserrcode={}, errtype={}, reqid={}", ase.toString(), ase.getStatusCode(), ase.getErrorCode(), ase.getErrorType(), ase.getRequestId()); } }
@Test public void testHandleErrorResponse() throws IOException { final Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(400).build(); HttpResponseHandler<AmazonServiceException> errorResponseHandler = new HttpResponseHandler<AmazonServiceException>() { @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 boolean needsConnectionLeftOpen() { return false; } }; AmazonServiceException e = client.handleErrorResponse(request, errorResponseHandler, httpResponse); assertEquals(e.getStatusCode(), 400); assertEquals(e.getErrorCode(), "TestError"); assertEquals(e.getErrorType(), ErrorType.Service); assertEquals(e.getServiceName(), "ServiceName"); }
private static void logException(AmazonServiceException ase) { logger.error("AmazonServiceException: error={}, statuscode={}, awserrcode={}, errtype={}, reqid={}", ase.toString(), ase.getStatusCode(), ase.getErrorCode(), ase.getErrorType(), ase.getRequestId()); }
String parseError(AmazonServiceException ase){ String message; message = "Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."; message += "Error Message: " + ase.getMessage(); message += "HTTP Status Code: " + ase.getStatusCode(); message += "AWS Error Code: " + ase.getErrorCode(); message += "Error Type: " + ase.getErrorType(); message += "Request ID: " + ase.getRequestId(); return message; } String parseError(AmazonClientException ace){ String message; message = "Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."; message += "Error Message: " + ace.getMessage(); return message; }