1 /*
2  * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.transform;
16
17 import static com.amazonaws.util.XpathUtils.asString;
18 import static com.amazonaws.util.XpathUtils.xpath;
19
20 import javax.xml.xpath.XPath;
21
22 import org.w3c.dom.Node;
23
24 import com.amazonaws.AmazonServiceException;
25 import com.amazonaws.AmazonServiceException.ErrorType;
26 import com.amazonaws.annotation.SdkProtectedApi;
27
28 /**
29  * Error unmarshaller that knows how to interpret a standard AWS error message
30  * (i.e. where to find the AWS error code, the error message, etc.) and turn it
31  * into an AmazonServiceException.
32  *
33  * @see LegacyErrorUnmarshaller
34  */

35 @SdkProtectedApi
36 public class StandardErrorUnmarshaller extends AbstractErrorUnmarshaller<Node> {
37
38     /**
39      * Constructs a new unmarshaller that will unmarshall a standard AWS error
40      * message as a generic AmazonServiceException object.
41      */

42     public StandardErrorUnmarshaller() {}
43
44     /**
45      * Constructor allowing subclasses to specify a specific type of
46      * AmazonServiceException to instantiating when populating the exception
47      * object with data from the error message.
48      *
49      * @param exceptionClass
50      *            The class of AmazonServiceException to create and populate
51      *            when unmarshalling the error message.
52      */

53     public StandardErrorUnmarshaller(Class<? extends AmazonServiceException> exceptionClass) {
54         super(exceptionClass);
55     }
56
57     /**
58      * @see com.amazonaws.transform.Unmarshaller#unmarshall(java.lang.Object)
59      */

60     public AmazonServiceException unmarshall(Node in) throws Exception {
61         XPath xpath = xpath();
62         String errorCode = parseErrorCode(in, xpath);
63         String errorType = asString("ErrorResponse/Error/Type", in, xpath);
64         String requestId = asString("ErrorResponse/RequestId", in, xpath);
65         String message = asString("ErrorResponse/Error/Message", in, xpath);
66
67         AmazonServiceException ase = newException(message);
68         ase.setErrorCode(errorCode);
69         ase.setRequestId(requestId);
70
71         if (errorType == null) {
72             ase.setErrorType(ErrorType.Unknown);
73         } else if (errorType.equalsIgnoreCase("Receiver")) {
74             ase.setErrorType(ErrorType.Service);
75         } else if (errorType.equalsIgnoreCase("Sender")) {
76             ase.setErrorType(ErrorType.Client);
77         }
78
79         return ase;
80     }
81
82     /**
83      * Returns the AWS error code for the specified error response.
84      *
85      * @param in
86      *            The DOM tree node containing the error response.
87      *
88      * @return The AWS error code contained in the specified error response.
89      *
90      * @throws Exception
91      *             If any problems were encountered pulling out the AWS error
92      *             code.
93      */

94     public String parseErrorCode(Node in) throws Exception {
95         return asString("ErrorResponse/Error/Code", in);
96     }
97
98     public String parseErrorCode(Node in, XPath xpath) throws Exception {
99         return asString("ErrorResponse/Error/Code", in, xpath);
100     }
101
102     /**
103      * Returns the path to the specified property within an error response.
104      *
105      * @param property
106      *            The name of the desired property.
107      *
108      * @return The path to the specified property within an error message.
109      */

110     public String getErrorPropertyPath(String property) {
111         return "ErrorResponse/Error/" + property;
112     }
113
114 }
115