1 /* 2 * Copyright 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 16 package software.amazon.awssdk.core.exception; 17 18 import java.util.Collections; 19 import java.util.List; 20 import software.amazon.awssdk.annotations.SdkPublicApi; 21 import software.amazon.awssdk.core.SdkField; 22 import software.amazon.awssdk.core.SdkPojo; 23 import software.amazon.awssdk.http.HttpStatusCode; 24 25 /** 26 * Extension of SdkException that represents an error response returned by 27 * the requested downstream service. Receiving an exception of this type indicates that 28 * the caller's request was correctly transmitted to the service, but for some 29 * reason, the service was not able to process it, and returned an error 30 * response instead. 31 * <p> 32 * Exceptions that extend {@link SdkServiceException} are assumed to be able to be 33 * successfully retried. 34 * <p> 35 * SdkServiceException provides callers several pieces of information that can 36 * be used to obtain more information about the error and why it occurred. 37 * 38 * @see SdkClientException 39 */ 40 @SdkPublicApi 41 public class SdkServiceException extends SdkException implements SdkPojo { 42 43 private final String requestId; 44 private final String extendedRequestId; 45 private final int statusCode; 46 SdkServiceException(Builder b)47 protected SdkServiceException(Builder b) { 48 super(b); 49 this.requestId = b.requestId(); 50 this.extendedRequestId = b.extendedRequestId(); 51 this.statusCode = b.statusCode(); 52 } 53 54 /** 55 * The requestId that was returned by the called service. 56 * @return String containing the requestId 57 */ requestId()58 public String requestId() { 59 return requestId; 60 } 61 62 /** 63 * The extendedRequestId that was returned by the called service. 64 * @return String ctontaining the extendedRequestId 65 */ extendedRequestId()66 public String extendedRequestId() { 67 return extendedRequestId; 68 } 69 70 /** 71 * The status code that was returned by the called service. 72 * @return int containing the status code. 73 */ statusCode()74 public int statusCode() { 75 return statusCode; 76 } 77 78 /** 79 * Specifies whether or not an exception may have been caused by clock skew. 80 */ isClockSkewException()81 public boolean isClockSkewException() { 82 return false; 83 } 84 85 /** 86 * Specifies whether or not an exception is caused by throttling. 87 * 88 * @return true if the status code is 429, otherwise false. 89 */ isThrottlingException()90 public boolean isThrottlingException() { 91 return statusCode == HttpStatusCode.THROTTLING; 92 } 93 94 /** 95 * @return {@link Builder} instance to construct a new {@link SdkServiceException}. 96 */ builder()97 public static Builder builder() { 98 return new BuilderImpl(); 99 } 100 101 /** 102 * Create a {@link SdkServiceException.Builder} initialized with the properties of this {@code SdkServiceException}. 103 * 104 * @return A new builder initialized with this config's properties. 105 */ 106 @Override toBuilder()107 public Builder toBuilder() { 108 return new BuilderImpl(this); 109 } 110 serializableBuilderClass()111 public static Class<? extends Builder> serializableBuilderClass() { 112 return BuilderImpl.class; 113 } 114 115 @Override sdkFields()116 public List<SdkField<?>> sdkFields() { 117 return Collections.emptyList(); 118 } 119 120 public interface Builder extends SdkException.Builder, SdkPojo { 121 @Override message(String message)122 Builder message(String message); 123 124 @Override cause(Throwable cause)125 Builder cause(Throwable cause); 126 127 @Override writableStackTrace(Boolean writableStackTrace)128 Builder writableStackTrace(Boolean writableStackTrace); 129 130 /** 131 * Specifies the requestId returned by the called service. 132 * 133 * @param requestId A string that identifies the request made to a service. 134 * @return This object for method chaining. 135 */ requestId(String requestId)136 Builder requestId(String requestId); 137 138 /** 139 * The requestId returned by the called service. 140 * 141 * @return String containing the requestId 142 */ requestId()143 String requestId(); 144 145 /** 146 * Specifies the extendedRequestId returned by the called service. 147 * 148 * @param extendedRequestId A string that identifies the request made to a service. 149 * @return This object for method chaining. 150 */ extendedRequestId(String extendedRequestId)151 Builder extendedRequestId(String extendedRequestId); 152 153 /** 154 * The extendedRequestId returned by the called service. 155 * 156 * @return String containing the extendedRequestId 157 */ extendedRequestId()158 String extendedRequestId(); 159 160 /** 161 * Specifies the status code returned by the service. 162 * 163 * @param statusCode an int containing the status code returned by the service. 164 * @return This method for object chaining. 165 */ statusCode(int statusCode)166 Builder statusCode(int statusCode); 167 168 /** 169 * The status code returned by the service. 170 * @return int containing the status code 171 */ statusCode()172 int statusCode(); 173 174 /** 175 * Creates a new {@link SdkServiceException} with the specified properties. 176 * 177 * @return The new {@link SdkServiceException}. 178 */ 179 @Override build()180 SdkServiceException build(); 181 } 182 183 protected static class BuilderImpl extends SdkException.BuilderImpl implements Builder { 184 185 protected String requestId; 186 protected String extendedRequestId; 187 protected int statusCode; 188 BuilderImpl()189 protected BuilderImpl() { 190 } 191 BuilderImpl(SdkServiceException ex)192 protected BuilderImpl(SdkServiceException ex) { 193 super(ex); 194 this.requestId = ex.requestId(); 195 this.extendedRequestId = ex.extendedRequestId(); 196 this.statusCode = ex.statusCode(); 197 } 198 199 @Override message(String message)200 public Builder message(String message) { 201 this.message = message; 202 return this; 203 } 204 205 @Override cause(Throwable cause)206 public Builder cause(Throwable cause) { 207 this.cause = cause; 208 return this; 209 } 210 211 @Override writableStackTrace(Boolean writableStackTrace)212 public Builder writableStackTrace(Boolean writableStackTrace) { 213 this.writableStackTrace = writableStackTrace; 214 return this; 215 } 216 217 @Override requestId(String requestId)218 public Builder requestId(String requestId) { 219 this.requestId = requestId; 220 return this; 221 } 222 223 @Override extendedRequestId(String extendedRequestId)224 public Builder extendedRequestId(String extendedRequestId) { 225 this.extendedRequestId = extendedRequestId; 226 return this; 227 } 228 229 @Override requestId()230 public String requestId() { 231 return requestId; 232 } 233 getRequestId()234 public String getRequestId() { 235 return requestId; 236 } 237 setRequestId(String requestId)238 public void setRequestId(String requestId) { 239 this.requestId = requestId; 240 } 241 242 @Override extendedRequestId()243 public String extendedRequestId() { 244 return extendedRequestId; 245 } 246 getExtendedRequestId()247 public String getExtendedRequestId() { 248 return extendedRequestId; 249 } 250 setExtendedRequestId(String extendedRequestId)251 public void setExtendedRequestId(String extendedRequestId) { 252 this.extendedRequestId = extendedRequestId; 253 } 254 255 @Override statusCode(int statusCode)256 public Builder statusCode(int statusCode) { 257 this.statusCode = statusCode; 258 return this; 259 } 260 getStatusCode()261 public int getStatusCode() { 262 return statusCode; 263 } 264 setStatusCode(int statusCode)265 public void setStatusCode(int statusCode) { 266 this.statusCode = statusCode; 267 } 268 269 @Override statusCode()270 public int statusCode() { 271 return statusCode; 272 } 273 274 @Override build()275 public SdkServiceException build() { 276 return new SdkServiceException(this); 277 } 278 279 @Override sdkFields()280 public List<SdkField<?>> sdkFields() { 281 return Collections.emptyList(); 282 } 283 } 284 } 285