Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2011-2015, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 package com.amazonaws.transform;
Unmarshaller for JSON error responses from AWS services.
     public JsonErrorUnmarshaller() {}
     protected JsonErrorUnmarshaller(Class<? extends AmazonServiceExceptionexceptionClass) {

Subclass should override the match(String, JSONObject) method to indicate whether it represents the given error type, and unmarshall(JSONObject) should never return null.
     public AmazonServiceException unmarshall(JSONObject jsonthrows Exception {
         String message = parseMessage(json);
         String errorCode = parseErrorCode(json);
         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);
             return ase;
     public String parseMessage(JSONObject jsonthrows Exception {
         return parseMember("message"json);
     public String parseMember(String keyJSONObject jsonthrows JSONException {
         if (key == null || key.length() == 0) {
             return null;
         String firstLetterUppercaseKey;
         String firstLetterLowercaseKey;
         firstLetterLowercaseKey = key.substring(0, 1).toLowerCase()
                 + key.substring(1);
         firstLetterUppercaseKey = key.substring(0, 1).toUpperCase()
                 + key.substring(1);
          String value = "";
          if (json.has(firstLetterUppercaseKey)) {
              value = json.getString(firstLetterUppercaseKey);
          } else if (json.has(firstLetterLowercaseKey)) {
              value = json.getString(firstLetterLowercaseKey);
          return value;
     public String parseErrorCode(JSONObject jsonthrows Exception {
         if (json.has("__type")) {
             String type = json.getString("__type");
             int separator = type.lastIndexOf("#");
             return type.substring(separator + 1);
         return null;

Any subclass that is specific to a error type should only return true when the response matches, either by matching the error type parsed from header or from the JSON content.

errorTypeFromHeader The error type parsed from the response headers, or null if such information is not available in the headers.
json The JSON content of the response. Subclass should check for the error type information from this JSONObject if errorTypeFromHeader is null.
    public boolean match(String errorTypeFromHeaderJSONObject jsonthrows Exception {
        return true;
New to GrepCode? Check out our FAQ X