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; } }
@Override public AmazonServiceException handle(HttpResponse errorResponse) throws Exception { AmazonServiceException ase = createAse(errorResponse); if (ase == null) { throw new SdkClientException("Unable to unmarshall error response from service"); } ase.setHttpHeaders(errorResponse.getHeaders()); if (StringUtils.isNullOrEmpty(ase.getErrorCode())) { ase.setErrorCode(errorResponse.getStatusCode() + " " + errorResponse.getStatusText()); } 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; }
static Exception convert(BatchResultErrorEntry be) { AmazonServiceException toReturn = new AmazonServiceException(be.getMessage()); toReturn.setErrorCode(be.getCode()); toReturn.setErrorType(be.isSenderFault() ? ErrorType.Client : ErrorType.Service); toReturn.setServiceName("AmazonSQS"); return toReturn; }
/** * @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; }
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 AmazonServiceException getServiceException(String errorCode) { AmazonServiceException ase = new AmazonServiceException("some error message"); ase.setErrorCode(errorCode); return ase; } }
private AmazonServiceException getServiceException(String errorCode) { AmazonServiceException ase = new AmazonServiceException("some error message"); ase.setErrorCode(errorCode); return ase; } }
/** * Subclass should override the match(String, JSONObject) method to indicate * whether it represents the given error type, and unmarshall(JSONObject) * should never return null. */ @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { String message = error.getMessage(); String errorCode = error.getErrorCode(); if ((null == message || message.isEmpty()) && (null == errorCode || errorCode.isEmpty())) { /** * Trigger the catch block in AmazonHttpClient.handleErrorResponse * to handle 413 and 503 errors */ throw new AmazonClientException( "Neither error message nor error code is found in the error response payload."); } else { AmazonServiceException ase = newException(message); ase.setErrorCode(errorCode); return ase; } }
@Override public AmazonServiceException unmarshall(Node in) throws Exception { String errorCode = parseErrorCode(in); String message = XpathUtils.asString("Response/Errors/Error/Message", in); String requestId = XpathUtils.asString("Response/RequestID", in); String errorType = XpathUtils.asString("Response/Errors/Error/Type", in); 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 ("server".equalsIgnoreCase(errorType)) { ase.setErrorType(ErrorType.Service); } else if ("client".equalsIgnoreCase(errorType)) { 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; }
@Override public AmazonServiceException handle(HttpResponse errorResponse) throws Exception { AmazonServiceException ase = createAse(errorResponse); if (ase == null) { throw new SdkClientException("Unable to unmarshall error response from service"); } ase.setHttpHeaders(errorResponse.getHeaders()); if (StringUtils.isNullOrEmpty(ase.getErrorCode())) { ase.setErrorCode(errorResponse.getStatusCode() + " " + errorResponse.getStatusText()); } return ase; }
/** * 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; }
static Exception convert(BatchResultErrorEntry be) { AmazonServiceException toReturn = new AmazonServiceException(be.getMessage()); toReturn.setErrorCode(be.getCode()); toReturn.setErrorType(be.isSenderFault() ? ErrorType.Client : ErrorType.Service); toReturn.setServiceName("AmazonSQS"); return toReturn; }
/** * @see com.amazonaws.transform.Unmarshaller#unmarshall(java.lang.Object) */ @Override public AmazonServiceException unmarshall(Node in) throws Exception { String errorCode = parseErrorCode(in); String errorType = XpathUtils.asString("ErrorResponse/Error/Type", in); String requestId = XpathUtils.asString("ErrorResponse/RequestId", in); String message = XpathUtils.asString("ErrorResponse/Error/Message", in); AmazonServiceException ase = newException(message); ase.setErrorCode(errorCode); ase.setRequestId(requestId); if (errorType == null) { ase.setErrorType(ErrorType.Unknown); } else if ("Receiver".equalsIgnoreCase(errorType)) { ase.setErrorType(ErrorType.Service); } else if ("Sender".equalsIgnoreCase(errorType)) { ase.setErrorType(ErrorType.Client); } return ase; }
/** * @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 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; }
@Test public void testContainsThrottlingException() { List<FailedBatch> failedBatches = new ArrayList<FailedBatch>(); FailedBatch nonThrottle = new FailedBatch(); nonThrottle.setException(new AmazonServiceException("InvalidInput")); failedBatches.add(nonThrottle); assertFalse(mapper.containsThrottlingException(failedBatches)); FailedBatch throttle = new FailedBatch(); AmazonServiceException ase = new AmazonServiceException("ThrottlingException"); ase.setErrorCode("ThrottlingException"); nonThrottle.setException(ase); failedBatches.add(throttle); assertTrue(mapper.containsThrottlingException(failedBatches)); }
@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; }