Skip to content

Commit 6636e23

Browse files
author
Ajay Kannan
committed
Create BaseServiceException in gcloud-java-core
1 parent cb64ccd commit 6636e23

6 files changed

Lines changed: 135 additions & 51 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 service exception.
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+
public boolean retryable() {
49+
return retryable;
50+
}
51+
}
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+
}

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

Lines changed: 26 additions & 24 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, ErrorInfo> REASON_TO_CODE;
34+
private static final ImmutableMap<Integer, ErrorInfo> HTTP_TO_CODE;
3435

35-
private final Code code;
36+
private final ErrorInfo errorInfo;
3637

3738
/**
38-
* An error code to represent the failure.
39+
* Represent metadata about {@link DatastoreException}s.
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 ErrorInfo {
4445

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

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

64-
Code(boolean retryable, String description, int httpStatus) {
65+
ErrorInfo(boolean retryable, String description, int httpStatus) {
6566
this.retryable = retryable;
6667
this.description = description;
6768
this.httpStatus = httpStatus;
@@ -89,30 +90,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
8990
}
9091

9192
static {
92-
ImmutableMap.Builder<String, Code> builder = ImmutableMap.builder();
93-
Map<Integer, Code> httpCodes = new HashMap<>();
94-
for (Code code : Code.values()) {
93+
ImmutableMap.Builder<String, ErrorInfo> builder = ImmutableMap.builder();
94+
Map<Integer, ErrorInfo> httpCodes = new HashMap<>();
95+
for (ErrorInfo code : ErrorInfo.values()) {
9596
builder.put(code.name(), code);
9697
httpCodes.put(code.httpStatus(), code);
9798
}
9899
REASON_TO_CODE = builder.build();
99100
HTTP_TO_CODE = ImmutableMap.copyOf(httpCodes);
100101
}
101102

102-
public DatastoreException(Code code, String message, Exception cause) {
103-
super(MoreObjects.firstNonNull(message, code.description), cause);
104-
this.code = code;
103+
public DatastoreException(ErrorInfo errorInfo, String message, Exception cause) {
104+
super(errorInfo.httpStatus(), MoreObjects.firstNonNull(message, errorInfo.description),
105+
errorInfo.retryable(), cause);
106+
this.errorInfo = errorInfo;
105107
}
106108

107-
public DatastoreException(Code code, String message) {
108-
this(code, message, null);
109+
public DatastoreException(ErrorInfo errorInfo, String message) {
110+
this(errorInfo, message, null);
109111
}
110112

111113
/**
112114
* Returns the code associated with this exception.
113115
*/
114-
public Code code() {
115-
return code;
116+
public ErrorInfo errorInfo() {
117+
return errorInfo;
116118
}
117119

118120
static DatastoreException translateAndThrow(RetryHelperException ex) {
@@ -122,7 +124,7 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
122124
if (ex instanceof RetryHelper.RetryInterruptedException) {
123125
RetryHelper.RetryInterruptedException.propagate();
124126
}
125-
throw new DatastoreException(Code.UNKNOWN, ex.getMessage(), ex);
127+
throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex);
126128
}
127129

128130
/**
@@ -133,9 +135,9 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
133135
*/
134136
static DatastoreException translateAndThrow(DatastoreRpcException exception) {
135137
String message = exception.getMessage();
136-
Code code = REASON_TO_CODE.get(exception.reason());
138+
ErrorInfo code = REASON_TO_CODE.get(exception.reason());
137139
if (code == null) {
138-
code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), Code.UNKNOWN);
140+
code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), ErrorInfo.UNKNOWN);
139141
}
140142
throw code.translate(exception, message);
141143
}
@@ -147,10 +149,10 @@ static DatastoreException translateAndThrow(DatastoreRpcException exception) {
147149
* @throws DatastoreException every time
148150
*/
149151
static DatastoreException throwInvalidRequest(String massage, Object... params) {
150-
throw new DatastoreException(Code.FAILED_PRECONDITION, String.format(massage, params));
152+
throw new DatastoreException(ErrorInfo.FAILED_PRECONDITION, String.format(massage, params));
151153
}
152154

153155
static DatastoreException propagateUserException(Exception ex) {
154-
throw new DatastoreException(Code.UNKNOWN, ex.getMessage(), ex);
156+
throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex);
155157
}
156158
}

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.Code;
22+
import com.google.gcloud.datastore.DatastoreException.ErrorInfo;
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-
Code code = Code.valueOf(reason.name());
33+
ErrorInfo code = ErrorInfo.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(Code.ABORTED, "bla");
40-
assertEquals(Code.ABORTED, exception.code());
39+
DatastoreException exception = new DatastoreException(ErrorInfo.ABORTED, "bla");
40+
assertEquals(ErrorInfo.ABORTED, exception.errorInfo());
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.errorInfo().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(ErrorInfo.FAILED_PRECONDITION, ex.errorInfo());
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.Code.ABORTED, expected.code());
200+
assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo());
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.ErrorInfo.ABORTED, expected.errorInfo());
229229
}
230230
}
231231

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)