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 software.amazon.awssdk.annotations.SdkPublicApi; 19 import software.amazon.awssdk.utils.builder.Buildable; 20 21 /** 22 * Base class for all exceptions thrown by the SDK. 23 * 24 * @see SdkServiceException 25 * @see SdkClientException 26 */ 27 @SdkPublicApi 28 public class SdkException extends RuntimeException { 29 30 private static final long serialVersionUID = 1L; 31 SdkException(Builder builder)32 protected SdkException(Builder builder) { 33 super(messageFromBuilder(builder), builder.cause(), true, writableStackTraceFromBuilder(builder)); 34 } 35 36 /** 37 * Use the message from the builder, if it's specified, otherwise inherit the message from the "cause" exception. 38 */ messageFromBuilder(Builder builder)39 private static String messageFromBuilder(Builder builder) { 40 if (builder.message() != null) { 41 return builder.message(); 42 } 43 44 if (builder.cause() != null) { 45 return builder.cause().getMessage(); 46 } 47 48 return null; 49 } 50 writableStackTraceFromBuilder(Builder builder)51 private static boolean writableStackTraceFromBuilder(Builder builder) { 52 return builder.writableStackTrace() == null || builder.writableStackTrace(); 53 } 54 create(String message, Throwable cause)55 public static SdkException create(String message, Throwable cause) { 56 return SdkException.builder().message(message).cause(cause).build(); 57 } 58 59 /** 60 * Specifies whether or not an exception can be expected to succeed on a retry. 61 */ retryable()62 public boolean retryable() { 63 return false; 64 } 65 66 /** 67 * Create a {@link SdkException.Builder} initialized with the properties of this {@code SdkException}. 68 * 69 * @return A new builder initialized with this config's properties. 70 */ toBuilder()71 public Builder toBuilder() { 72 return new BuilderImpl(this); 73 } 74 75 /** 76 * @return {@link Builder} instance to construct a new {@link SdkException}. 77 */ builder()78 public static Builder builder() { 79 return new BuilderImpl(); 80 } 81 82 public interface Builder extends Buildable { 83 /** 84 * Specifies the exception that caused this exception to occur. 85 * 86 * @param cause The exception that caused this exception to occur. 87 * @return This object for method chaining. 88 */ cause(Throwable cause)89 Builder cause(Throwable cause); 90 91 /** 92 * The exception that caused this exception to occur. 93 * 94 * @return The exception that caused this exception to occur. 95 */ cause()96 Throwable cause(); 97 98 /** 99 * Specifies the details of this exception. 100 * 101 * @param message The details of this exception. 102 * @return This method for object chaining 103 */ message(String message)104 Builder message(String message); 105 106 /** 107 * The details of this exception. 108 * 109 * @return Details of this exception. 110 */ message()111 String message(); 112 113 /** 114 * Specifies whether the stack trace in this exception can be written. 115 * 116 * @param writableStackTrace Whether the stack trace can be written. 117 * @return This method for object chaining 118 */ writableStackTrace(Boolean writableStackTrace)119 Builder writableStackTrace(Boolean writableStackTrace); 120 121 /** 122 * Whether the stack trace in this exception can be written. 123 */ writableStackTrace()124 Boolean writableStackTrace(); 125 126 /** 127 * Creates a new {@link SdkException} with the specified properties. 128 * 129 * @return The new {@link SdkException}. 130 */ 131 @Override build()132 SdkException build(); 133 } 134 135 protected static class BuilderImpl implements Builder { 136 137 protected Throwable cause; 138 protected String message; 139 protected Boolean writableStackTrace; 140 BuilderImpl()141 protected BuilderImpl() { 142 } 143 BuilderImpl(SdkException ex)144 protected BuilderImpl(SdkException ex) { 145 this.cause = ex.getCause(); 146 this.message = ex.getMessage(); 147 } 148 149 getCause()150 public Throwable getCause() { 151 return cause; 152 } 153 setCause(Throwable cause)154 public void setCause(Throwable cause) { 155 this.cause = cause; 156 } 157 158 @Override cause(Throwable cause)159 public Builder cause(Throwable cause) { 160 this.cause = cause; 161 return this; 162 } 163 164 @Override cause()165 public Throwable cause() { 166 return cause; 167 } 168 getMessage()169 public String getMessage() { 170 return message; 171 } 172 173 @Override message(String message)174 public Builder message(String message) { 175 this.message = message; 176 return this; 177 } 178 setMessage(String message)179 public void setMessage(String message) { 180 this.message = message; 181 } 182 183 @Override message()184 public String message() { 185 return message; 186 } 187 188 @Override writableStackTrace(Boolean writableStackTrace)189 public Builder writableStackTrace(Boolean writableStackTrace) { 190 this.writableStackTrace = writableStackTrace; 191 return this; 192 } 193 setWritableStackTrace(Boolean writableStackTrace)194 public void setWritableStackTrace(Boolean writableStackTrace) { 195 this.writableStackTrace = writableStackTrace; 196 } 197 198 @Override writableStackTrace()199 public Boolean writableStackTrace() { 200 return writableStackTrace; 201 } 202 getWritableStackTrace()203 public Boolean getWritableStackTrace() { 204 return writableStackTrace; 205 } 206 207 @Override build()208 public SdkException build() { 209 return new SdkException(this); 210 } 211 212 } 213 } 214