Skip to content

Commit b59d10d

Browse files
author
Ajay Kannan
committed
Rename enum, fix docs, and make accessors package private
1 parent 6636e23 commit b59d10d

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.gcloud;
1818

1919
/**
20-
* Base service exception.
20+
* Base class for all service exceptions.
2121
*/
2222
public class BaseServiceException extends RuntimeException {
2323

@@ -45,6 +45,9 @@ public int code() {
4545
return code;
4646
}
4747

48+
/**
49+
* Returns {@code true} when it is safe to retry the operation that caused this exception.
50+
*/
4851
public boolean retryable() {
4952
return retryable;
5053
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030
public class DatastoreException extends BaseServiceException {
3131

3232
private static final long serialVersionUID = -2336749234060754893L;
33-
private static final ImmutableMap<String, ErrorInfo> REASON_TO_CODE;
34-
private static final ImmutableMap<Integer, ErrorInfo> HTTP_TO_CODE;
33+
private static final ImmutableMap<String, DatastoreError> REASON_TO_CODE;
34+
private static final ImmutableMap<Integer, DatastoreError> HTTP_TO_CODE;
3535

36-
private final ErrorInfo errorInfo;
36+
private final DatastoreError error;
3737

3838
/**
39-
* Represent metadata about {@link DatastoreException}s.
39+
* Represents Datastore errors.
4040
*
4141
* @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
4242
* Datastore error codes</a>
4343
*/
44-
public enum ErrorInfo {
44+
public enum DatastoreError {
4545

4646
ABORTED(Reason.ABORTED),
4747
DEADLINE_EXCEEDED(Reason.DEADLINE_EXCEEDED),
@@ -58,29 +58,25 @@ public enum ErrorInfo {
5858
private final String description;
5959
private final int httpStatus;
6060

61-
ErrorInfo(Reason reason) {
61+
DatastoreError(Reason reason) {
6262
this(reason.retryable(), reason.description(), reason.httpStatus());
6363
}
6464

65-
ErrorInfo(boolean retryable, String description, int httpStatus) {
65+
DatastoreError(boolean retryable, String description, int httpStatus) {
6666
this.retryable = retryable;
6767
this.description = description;
6868
this.httpStatus = httpStatus;
6969
}
7070

71-
public String description() {
71+
String description() {
7272
return description;
7373
}
7474

75-
public int httpStatus() {
75+
int httpStatus() {
7676
return httpStatus;
7777
}
7878

79-
/**
80-
* Returns {@code true} if this exception is transient and the same request could be retried.
81-
* For any retry it is highly recommended to apply an exponential backoff.
82-
*/
83-
public boolean retryable() {
79+
boolean retryable() {
8480
return retryable;
8581
}
8682

@@ -90,31 +86,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
9086
}
9187

9288
static {
93-
ImmutableMap.Builder<String, ErrorInfo> builder = ImmutableMap.builder();
94-
Map<Integer, ErrorInfo> httpCodes = new HashMap<>();
95-
for (ErrorInfo code : ErrorInfo.values()) {
89+
ImmutableMap.Builder<String, DatastoreError> builder = ImmutableMap.builder();
90+
Map<Integer, DatastoreError> httpCodes = new HashMap<>();
91+
for (DatastoreError code : DatastoreError.values()) {
9692
builder.put(code.name(), code);
9793
httpCodes.put(code.httpStatus(), code);
9894
}
9995
REASON_TO_CODE = builder.build();
10096
HTTP_TO_CODE = ImmutableMap.copyOf(httpCodes);
10197
}
10298

103-
public DatastoreException(ErrorInfo errorInfo, String message, Exception cause) {
99+
public DatastoreException(DatastoreError errorInfo, String message, Exception cause) {
104100
super(errorInfo.httpStatus(), MoreObjects.firstNonNull(message, errorInfo.description),
105101
errorInfo.retryable(), cause);
106-
this.errorInfo = errorInfo;
102+
this.error = errorInfo;
107103
}
108104

109-
public DatastoreException(ErrorInfo errorInfo, String message) {
105+
public DatastoreException(DatastoreError errorInfo, String message) {
110106
this(errorInfo, message, null);
111107
}
112108

113109
/**
114110
* Returns the code associated with this exception.
115111
*/
116-
public ErrorInfo errorInfo() {
117-
return errorInfo;
112+
public DatastoreError datastoreError() {
113+
return error;
118114
}
119115

120116
static DatastoreException translateAndThrow(RetryHelperException ex) {
@@ -124,7 +120,7 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
124120
if (ex instanceof RetryHelper.RetryInterruptedException) {
125121
RetryHelper.RetryInterruptedException.propagate();
126122
}
127-
throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex);
123+
throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex);
128124
}
129125

130126
/**
@@ -135,9 +131,9 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
135131
*/
136132
static DatastoreException translateAndThrow(DatastoreRpcException exception) {
137133
String message = exception.getMessage();
138-
ErrorInfo code = REASON_TO_CODE.get(exception.reason());
134+
DatastoreError code = REASON_TO_CODE.get(exception.reason());
139135
if (code == null) {
140-
code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), ErrorInfo.UNKNOWN);
136+
code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), DatastoreError.UNKNOWN);
141137
}
142138
throw code.translate(exception, message);
143139
}
@@ -149,10 +145,10 @@ static DatastoreException translateAndThrow(DatastoreRpcException exception) {
149145
* @throws DatastoreException every time
150146
*/
151147
static DatastoreException throwInvalidRequest(String massage, Object... params) {
152-
throw new DatastoreException(ErrorInfo.FAILED_PRECONDITION, String.format(massage, params));
148+
throw new DatastoreException(DatastoreError.FAILED_PRECONDITION, String.format(massage, params));
153149
}
154150

155151
static DatastoreException propagateUserException(Exception ex) {
156-
throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex);
152+
throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex);
157153
}
158154
}

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.fail;
2121

22-
import com.google.gcloud.datastore.DatastoreException.ErrorInfo;
22+
import com.google.gcloud.datastore.DatastoreException.DatastoreError;
2323
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException;
2424
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason;
2525

@@ -30,14 +30,14 @@ public class DatastoreExceptionTest {
3030
@Test
3131
public void testCode() throws Exception {
3232
for (Reason reason : Reason.values()) {
33-
ErrorInfo code = ErrorInfo.valueOf(reason.name());
33+
DatastoreError code = DatastoreError.valueOf(reason.name());
3434
assertEquals(reason.retryable(), code.retryable());
3535
assertEquals(reason.description(), code.description());
3636
assertEquals(reason.httpStatus(), code.httpStatus());
3737
}
3838

39-
DatastoreException exception = new DatastoreException(ErrorInfo.ABORTED, "bla");
40-
assertEquals(ErrorInfo.ABORTED, exception.errorInfo());
39+
DatastoreException exception = new DatastoreException(DatastoreError.ABORTED, "bla");
40+
assertEquals(DatastoreError.ABORTED, exception.datastoreError());
4141
}
4242

4343
@Test
@@ -47,7 +47,7 @@ public void testTranslateAndThrow() throws Exception {
4747
DatastoreException.translateAndThrow(new DatastoreRpcException(reason));
4848
fail("Exception expected");
4949
} catch (DatastoreException ex) {
50-
assertEquals(reason.name(), ex.errorInfo().name());
50+
assertEquals(reason.name(), ex.datastoreError().name());
5151
}
5252
}
5353
}
@@ -58,7 +58,7 @@ public void testThrowInvalidRequest() throws Exception {
5858
DatastoreException.throwInvalidRequest("message %s %d", "a", 1);
5959
fail("Exception expected");
6060
} catch (DatastoreException ex) {
61-
assertEquals(ErrorInfo.FAILED_PRECONDITION, ex.errorInfo());
61+
assertEquals(DatastoreError.FAILED_PRECONDITION, ex.datastoreError());
6262
assertEquals("message a 1", ex.getMessage());
6363
}
6464
}

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void testTransactionWithRead() {
197197
transaction.commit();
198198
fail("Expecting a failure");
199199
} catch (DatastoreException expected) {
200-
assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo());
200+
assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError());
201201
}
202202
}
203203

@@ -225,7 +225,7 @@ public void testTransactionWithQuery() {
225225
transaction.commit();
226226
fail("Expecting a failure");
227227
} catch (DatastoreException expected) {
228-
assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo());
228+
assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError());
229229
}
230230
}
231231

0 commit comments

Comments
 (0)