Skip to content

Commit 991477d

Browse files
committed
---
yaml --- r: 6413 b: refs/heads/tswast-patch-1 c: 1ea112d h: refs/heads/master i: 6411: cfbff95
1 parent edccfc5 commit 991477d

7 files changed

Lines changed: 156 additions & 72 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 73d379ba5afd65fa0a112093f48eace1e5acb931
60+
refs/heads/tswast-patch-1: 1ea112dff21e7c1833ebe47e009a4707f9d77915
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2015 Google Inc. 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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud;
18+
19+
/**
20+
* Base class for all service exceptions.
21+
*/
22+
public class BaseServiceException extends RuntimeException {
23+
24+
private static final long serialVersionUID = 5028833760039966178L;
25+
26+
private final int code;
27+
private final boolean retryable;
28+
29+
public BaseServiceException(int code, String message, boolean retryable) {
30+
super(message);
31+
this.code = code;
32+
this.retryable = retryable;
33+
}
34+
35+
public BaseServiceException(int code, String message, boolean retryable, Exception cause) {
36+
super(message, cause);
37+
this.code = code;
38+
this.retryable = retryable;
39+
}
40+
41+
/**
42+
* Returns the code associated with this exception.
43+
*/
44+
public int code() {
45+
return code;
46+
}
47+
48+
/**
49+
* Returns {@code true} when it is safe to retry the operation that caused this exception.
50+
*/
51+
public boolean retryable() {
52+
return retryable;
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2015 Google Inc. 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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import org.junit.Test;
22+
23+
/**
24+
* Tests for {@link BaseServiceException}.
25+
*/
26+
public class BaseServiceExceptionTest {
27+
28+
private final int code = 1;
29+
private final String message = "some message";
30+
private final boolean retryable = true;
31+
32+
@Test
33+
public void testBaseServiceException() {
34+
BaseServiceException serviceException = new BaseServiceException(code, message, retryable);
35+
assertEquals(serviceException.code(), code);
36+
assertEquals(serviceException.getMessage(), message);
37+
assertEquals(serviceException.getCause(), null);
38+
39+
Exception cause = new RuntimeException();
40+
serviceException = new BaseServiceException(code, message, retryable, cause);
41+
assertEquals(serviceException.code(), code);
42+
assertEquals(serviceException.getMessage(), message);
43+
assertEquals(serviceException.getCause(), cause);
44+
}
45+
}

branches/tswast-patch-1/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.base.MoreObjects;
2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.gcloud.BaseServiceException;
2122
import com.google.gcloud.RetryHelper;
2223
import com.google.gcloud.RetryHelper.RetryHelperException;
2324
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException;
@@ -26,21 +27,21 @@
2627
import java.util.HashMap;
2728
import java.util.Map;
2829

29-
public class DatastoreException extends RuntimeException {
30+
public class DatastoreException extends BaseServiceException {
3031

31-
private static final long serialVersionUID = 8170357898917041899L;
32-
private static final ImmutableMap<String, Code> REASON_TO_CODE;
33-
private static final ImmutableMap<Integer, Code> HTTP_TO_CODE;
32+
private static final long serialVersionUID = -2336749234060754893L;
33+
private static final ImmutableMap<String, DatastoreError> REASON_TO_ERROR;
34+
private static final ImmutableMap<Integer, DatastoreError> HTTP_TO_ERROR;
3435

35-
private final Code code;
36+
private final DatastoreError error;
3637

3738
/**
38-
* An error code to represent the failure.
39+
* Represents Datastore errors.
3940
*
4041
* @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
4142
* Datastore error codes</a>
4243
*/
43-
public enum Code {
44+
public enum DatastoreError {
4445

4546
ABORTED(Reason.ABORTED),
4647
DEADLINE_EXCEEDED(Reason.DEADLINE_EXCEEDED),
@@ -57,29 +58,25 @@ public enum Code {
5758
private final String description;
5859
private final int httpStatus;
5960

60-
Code(Reason reason) {
61+
DatastoreError(Reason reason) {
6162
this(reason.retryable(), reason.description(), reason.httpStatus());
6263
}
6364

64-
Code(boolean retryable, String description, int httpStatus) {
65+
DatastoreError(boolean retryable, String description, int httpStatus) {
6566
this.retryable = retryable;
6667
this.description = description;
6768
this.httpStatus = httpStatus;
6869
}
6970

70-
public String description() {
71+
String description() {
7172
return description;
7273
}
7374

74-
public int httpStatus() {
75+
int httpStatus() {
7576
return httpStatus;
7677
}
7778

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

@@ -89,30 +86,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
8986
}
9087

9188
static {
92-
ImmutableMap.Builder<String, Code> builder = ImmutableMap.builder();
93-
Map<Integer, Code> httpCodes = new HashMap<>();
94-
for (Code code : Code.values()) {
95-
builder.put(code.name(), code);
96-
httpCodes.put(code.httpStatus(), code);
89+
ImmutableMap.Builder<String, DatastoreError> builder = ImmutableMap.builder();
90+
Map<Integer, DatastoreError> httpCodes = new HashMap<>();
91+
for (DatastoreError error : DatastoreError.values()) {
92+
builder.put(error.name(), error);
93+
httpCodes.put(error.httpStatus(), error);
9794
}
98-
REASON_TO_CODE = builder.build();
99-
HTTP_TO_CODE = ImmutableMap.copyOf(httpCodes);
95+
REASON_TO_ERROR = builder.build();
96+
HTTP_TO_ERROR = ImmutableMap.copyOf(httpCodes);
10097
}
10198

102-
public DatastoreException(Code code, String message, Exception cause) {
103-
super(MoreObjects.firstNonNull(message, code.description), cause);
104-
this.code = code;
99+
public DatastoreException(DatastoreError error, String message, Exception cause) {
100+
super(error.httpStatus(), MoreObjects.firstNonNull(message, error.description),
101+
error.retryable(), cause);
102+
this.error = error;
105103
}
106104

107-
public DatastoreException(Code code, String message) {
108-
this(code, message, null);
105+
public DatastoreException(DatastoreError error, String message) {
106+
this(error, message, null);
109107
}
110108

111109
/**
112-
* Returns the code associated with this exception.
110+
* Returns the DatastoreError associated with this exception.
113111
*/
114-
public Code code() {
115-
return code;
112+
public DatastoreError datastoreError() {
113+
return error;
116114
}
117115

118116
static DatastoreException translateAndThrow(RetryHelperException ex) {
@@ -122,35 +120,36 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
122120
if (ex instanceof RetryHelper.RetryInterruptedException) {
123121
RetryHelper.RetryInterruptedException.propagate();
124122
}
125-
throw new DatastoreException(Code.UNKNOWN, ex.getMessage(), ex);
123+
throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex);
126124
}
127125

128126
/**
129-
* Translate DatastoreException to DatastoreException based on their
127+
* Translate DatastoreRpcExceptions to DatastoreExceptions based on their
130128
* HTTP error codes. This method will always throw a new DatastoreException.
131129
*
132130
* @throws DatastoreException every time
133131
*/
134132
static DatastoreException translateAndThrow(DatastoreRpcException exception) {
135133
String message = exception.getMessage();
136-
Code code = REASON_TO_CODE.get(exception.reason());
137-
if (code == null) {
138-
code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), Code.UNKNOWN);
134+
DatastoreError error = REASON_TO_ERROR.get(exception.reason());
135+
if (error == null) {
136+
error = MoreObjects.firstNonNull(
137+
HTTP_TO_ERROR.get(exception.httpStatus()), DatastoreError.UNKNOWN);
139138
}
140-
throw code.translate(exception, message);
139+
throw error.translate(exception, message);
141140
}
142141

143142
/**
144-
* Throw a DatastoreException with {@code FAILED_PRECONDITION} code and the {@code message}
143+
* Throw a DatastoreException with {@code FAILED_PRECONDITION} error and the {@code message}
145144
* in a nested exception.
146145
*
147146
* @throws DatastoreException every time
148147
*/
149148
static DatastoreException throwInvalidRequest(String massage, Object... params) {
150-
throw new DatastoreException(Code.FAILED_PRECONDITION, String.format(massage, params));
149+
throw new DatastoreException(DatastoreError.FAILED_PRECONDITION, String.format(massage, params));
151150
}
152151

153152
static DatastoreException propagateUserException(Exception ex) {
154-
throw new DatastoreException(Code.UNKNOWN, ex.getMessage(), ex);
153+
throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex);
155154
}
156155
}

branches/tswast-patch-1/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java

Lines changed: 10 additions & 10 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.Code;
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

@@ -28,16 +28,16 @@
2828
public class DatastoreExceptionTest {
2929

3030
@Test
31-
public void testCode() throws Exception {
31+
public void testDatastoreError() throws Exception {
3232
for (Reason reason : Reason.values()) {
33-
Code code = Code.valueOf(reason.name());
34-
assertEquals(reason.retryable(), code.retryable());
35-
assertEquals(reason.description(), code.description());
36-
assertEquals(reason.httpStatus(), code.httpStatus());
33+
DatastoreError error = DatastoreError.valueOf(reason.name());
34+
assertEquals(reason.retryable(), error.retryable());
35+
assertEquals(reason.description(), error.description());
36+
assertEquals(reason.httpStatus(), error.httpStatus());
3737
}
3838

39-
DatastoreException exception = new DatastoreException(Code.ABORTED, "bla");
40-
assertEquals(Code.ABORTED, exception.code());
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.code().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(Code.FAILED_PRECONDITION, ex.code());
61+
assertEquals(DatastoreError.FAILED_PRECONDITION, ex.datastoreError());
6262
assertEquals("message a 1", ex.getMessage());
6363
}
6464
}

branches/tswast-patch-1/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.Code.ABORTED, expected.code());
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.Code.ABORTED, expected.code());
228+
assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError());
229229
}
230230
}
231231

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageException.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,24 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import com.google.gcloud.BaseServiceException;
1920
import com.google.gcloud.RetryHelper;
2021
import com.google.gcloud.RetryHelper.RetryHelperException;
22+
import com.google.gcloud.RetryHelper.RetryInterruptedException;
2123

2224
/**
2325
* Storage service exception.
2426
*
2527
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/status-codes">Google Cloud
2628
* Storage error codes</a>
2729
*/
28-
public class StorageException extends RuntimeException {
30+
public class StorageException extends BaseServiceException {
2931

30-
private static final long serialVersionUID = -3748432005065428084L;
32+
private static final long serialVersionUID = 8088235105953640145L;
3133
private static final int UNKNOWN_CODE = -1;
3234

33-
private final int code;
34-
private final boolean retryable;
35-
3635
public StorageException(int code, String message, boolean retryable) {
37-
super(message);
38-
this.code = code;
39-
this.retryable = retryable;
40-
}
41-
42-
/**
43-
* Returns the code associated with this exception.
44-
*/
45-
public int code() {
46-
return code;
47-
}
48-
49-
public boolean retryable() {
50-
return retryable;
36+
super(code, message, retryable);
5137
}
5238

5339
/**

0 commit comments

Comments
 (0)