@@ -92,10 +92,7 @@ private String parseErrorCodeFromHeader(SdkHttpFullResponse response) {
9292
9393 private String parseErrorCodeFromXAmzErrorType (String headerValue ) {
9494 if (headerValue != null ) {
95- int separator = headerValue .indexOf (':' );
96- if (separator != -1 ) {
97- headerValue = headerValue .substring (0 , separator );
98- }
95+ return parseErrorCode (headerValue );
9996 }
10097 return headerValue ;
10198 }
@@ -119,50 +116,34 @@ private String parseErrorCodeFromContents(JsonNode jsonContents) {
119116 if (errorCodeField == null ) {
120117 return null ;
121118 }
122- String code = errorCodeField .text ();
123- // now extract the error code from the field contents following the smithy defined rules:
124- // 1) If a : character is present, then take only the contents before the first : character in the value.
125- // 2) If a # character is present, then take only the contents after the first # character in the value.
126- // see: https://smithy.io/2.0/aws/protocols/aws-json-1_1-protocol.html#operation-error-serialization
127- int start = 0 ;
128- int end = code .length ();
129-
130- // 1 - everything before the first ':'
131- int colonIndex = code .indexOf (':' );
132- if (colonIndex >= 0 ) {
133- end = colonIndex ;
134- }
135-
136- // 2 - everything after the first '#'
137- int hashIndex = code .indexOf ('#' );
138- if (hashIndex >= 0 && hashIndex + 1 < end ) {
139- start = hashIndex + 1 ;
140- }
141-
142- return code .substring (start , end );
119+ return parseErrorCode (errorCodeField .text ());
143120 }
144121
145- public static String parseErrorCode (String value ) {
122+ // Extract the error code from the error code contents following the smithy defined rules:
123+ // 1) If a : character is present, then take only the contents before the first : character in the value.
124+ // 2) If a # character is present, then take only the contents after the first # character in the value.
125+ // see: https://smithy.io/2.0/aws/protocols/aws-json-1_1-protocol.html#operation-error-serialization
126+ private static String parseErrorCode (String value ) {
146127 if (value == null || value .isEmpty ()) {
147128 return value ;
148129 }
149130
150131 int start = 0 ;
151132 int end = value .length ();
152133
153- // Step 1: everything before the first ':'
134+ // 1 - everything before the first ':'
154135 int colonIndex = value .indexOf (':' );
155136 if (colonIndex >= 0 ) {
156137 end = colonIndex ;
157138 }
158139
159- // Step 2: everything after the first '#'
140+ // 2 - everything after the first '#'
160141 int hashIndex = value .indexOf ('#' );
161142 if (hashIndex >= 0 && hashIndex + 1 < end ) {
162143 start = hashIndex + 1 ;
163144 }
164145
165- // Fast-path: return original string if unchanged
146+ // return original string if unchanged
166147 if (start == 0 && end == value .length ()) {
167148 return value ;
168149 }
0 commit comments