Skip to content

Commit ae3e446

Browse files
committed
Added error flag for indicating if the request was rejected.
1 parent 35d9aee commit ae3e446

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ protected static final class Error implements Serializable {
4848

4949
private final Integer code;
5050
private final String reason;
51+
private final boolean rejected;
5152

5253
public Error(Integer code, String reason) {
54+
this(code, reason, false);
55+
}
56+
57+
public Error(Integer code, String reason, boolean rejected) {
5358
this.code = code;
5459
this.reason = reason;
60+
this.rejected = rejected;
5561
}
5662

5763
/**
@@ -61,17 +67,26 @@ public Integer code() {
6167
return code;
6268
}
6369

70+
/**
71+
* Returns true if the error indicates that the API call was certainly not accepted by the
72+
* server.
73+
*/
74+
public boolean rejected() {
75+
return rejected;
76+
}
77+
6478
/**
6579
* Returns the reason that caused the exception.
6680
*/
6781
public String reason() {
6882
return reason;
6983
}
7084

71-
boolean isRetryable(Set<Error> retryableErrors) {
85+
boolean isRetryable(boolean idempotent, Set<Error> retryableErrors) {
7286
for (Error retryableError : retryableErrors) {
73-
if ((retryableError.code() == null || retryableError.code().equals(this.code()))
74-
&& (retryableError.reason() == null || retryableError.reason().equals(this.reason()))) {
87+
boolean match = (retryableError.code() == null || retryableError.code().equals(this.code()))
88+
&& (retryableError.reason() == null || retryableError.reason().equals(this.reason()));
89+
if (match && idempotent || match && retryableError.rejected()) {
7590
return true;
7691
}
7792
}
@@ -111,7 +126,7 @@ public BaseServiceException(IOException exception, boolean idempotent) {
111126
}
112127
}
113128
this.code = code;
114-
this.retryable = idempotent && isRetryable(exception);
129+
this.retryable = isRetryable(idempotent, exception);
115130
this.reason = reason;
116131
this.idempotent = idempotent;
117132
this.location = location;
@@ -123,7 +138,7 @@ public BaseServiceException(GoogleJsonError error, boolean idempotent) {
123138
this.code = error.getCode();
124139
this.reason = reason(error);
125140
this.idempotent = idempotent;
126-
this.retryable = idempotent && isRetryable(error);
141+
this.retryable = isRetryable(idempotent, error);
127142
this.location = null;
128143
this.debugInfo = null;
129144
}
@@ -138,7 +153,7 @@ public BaseServiceException(int code, String message, String reason, boolean ide
138153
this.code = code;
139154
this.reason = reason;
140155
this.idempotent = idempotent;
141-
this.retryable = idempotent && new Error(code, reason).isRetryable(retryableErrors());
156+
this.retryable = new Error(code, reason).isRetryable(idempotent, retryableErrors());
142157
this.location = null;
143158
this.debugInfo = null;
144159
}
@@ -147,15 +162,15 @@ protected Set<Error> retryableErrors() {
147162
return Collections.emptySet();
148163
}
149164

150-
protected boolean isRetryable(GoogleJsonError error) {
151-
return error != null && error(error).isRetryable(retryableErrors());
165+
protected boolean isRetryable(boolean idempotent, GoogleJsonError error) {
166+
return error != null && error(error).isRetryable(idempotent, retryableErrors());
152167
}
153168

154-
protected boolean isRetryable(IOException exception) {
169+
protected boolean isRetryable(boolean idempotent, IOException exception) {
155170
if (exception instanceof GoogleJsonResponseException) {
156-
return isRetryable(((GoogleJsonResponseException) exception).getDetails());
171+
return isRetryable(idempotent, (((GoogleJsonResponseException) exception).getDetails()));
157172
}
158-
return exception instanceof SocketTimeoutException;
173+
return idempotent && exception instanceof SocketTimeoutException;
159174
}
160175

161176
/**
@@ -187,8 +202,8 @@ public boolean idempotent() {
187202
}
188203

189204
/**
190-
* Returns the service location where the error causing the exception occurred. Returns
191-
* {@code null} if not set.
205+
* Returns the service location where the error causing the exception occurred. Returns {@code
206+
* null} if not set.
192207
*/
193208
public String location() {
194209
return location;
@@ -224,7 +239,7 @@ public int hashCode() {
224239
}
225240

226241
protected static String reason(GoogleJsonError error) {
227-
if (error.getErrors() != null && !error.getErrors().isEmpty()) {
242+
if (error.getErrors() != null && !error.getErrors().isEmpty()) {
228243
return error.getErrors().get(0).getReason();
229244
}
230245
return null;

gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public class DnsException extends BaseServiceException {
3131

3232
// see: https://cloud.google.com/dns/troubleshooting
3333
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(
34-
new Error(429, null),
35-
new Error(500, null),
36-
new Error(502, null),
37-
new Error(503, null),
38-
new Error(null, "userRateLimitExceeded"),
39-
new Error(null, "rateLimitExceeded"));
34+
new Error(429, null, true),
35+
new Error(500, null, false),
36+
new Error(502, null, false),
37+
new Error(503, null, false),
38+
new Error(null, "userRateLimitExceeded", true),
39+
new Error(null, "rateLimitExceeded", true));
4040
private static final long serialVersionUID = 490302380416260252L;
4141

4242
public DnsException(IOException exception, boolean idempotent) {

0 commit comments

Comments
 (0)