Skip to content

Commit 5f5ccf6

Browse files
committed
Chain cause exception to service exception in translateAndThrow
1 parent 5e0de55 commit 5f5ccf6

File tree

10 files changed

+181
-19
lines changed

10 files changed

+181
-19
lines changed

gcloud-java-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryException.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public class BigQueryException extends BaseServiceException {
4444
private final BigQueryError error;
4545

4646
public BigQueryException(int code, String message) {
47-
this(code, message, null);
47+
this(code, message, (Throwable) null);
48+
}
49+
50+
private BigQueryException(int code, String message, Throwable cause) {
51+
super(code, message, null, true, cause);
52+
this.error = null;
4853
}
4954

5055
public BigQueryException(int code, String message, BigQueryError error) {
@@ -100,6 +105,6 @@ public int hashCode() {
100105
*/
101106
static BaseServiceException translateAndThrow(RetryHelperException ex) {
102107
BaseServiceException.translateAndPropagateIfPossible(ex);
103-
throw new BigQueryException(UNKNOWN_CODE, ex.getMessage());
108+
throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
104109
}
105110
}

gcloud-java-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testBigqueryException() {
9797

9898
@Test
9999
public void testTranslateAndThrow() throws Exception {
100-
BigQueryException cause = new BigQueryException(503, "message");
100+
Exception cause = new BigQueryException(503, "message");
101101
RetryHelperException exceptionMock = createMock(RetryHelperException.class);
102102
expect(exceptionMock.getCause()).andReturn(cause).times(2);
103103
replay(exceptionMock);
@@ -111,5 +111,21 @@ public void testTranslateAndThrow() throws Exception {
111111
} finally {
112112
verify(exceptionMock);
113113
}
114+
cause = new IllegalArgumentException("message");
115+
exceptionMock = createMock(RetryHelperException.class);
116+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
117+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
118+
replay(exceptionMock);
119+
try {
120+
BigQueryException.translateAndThrow(exceptionMock);
121+
} catch (BaseServiceException ex) {
122+
assertEquals(BigQueryException.UNKNOWN_CODE, ex.code());
123+
assertEquals("message", ex.getMessage());
124+
assertFalse(ex.retryable());
125+
assertTrue(ex.idempotent());
126+
assertEquals(cause, ex.getCause());
127+
} finally {
128+
verify(exceptionMock);
129+
}
114130
}
115131
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public class DatastoreException extends BaseServiceException {
3737
new Error(10, "ABORTED"), new Error(4, "DEADLINE_EXCEEDED"), new Error(14, "UNAVAILABLE"));
3838
private static final long serialVersionUID = 2663750991205874435L;
3939

40-
public DatastoreException(int code, String message, String reason, Throwable cause) {
41-
super(code, message, reason, true, cause);
40+
public DatastoreException(int code, String message, String reason) {
41+
this(code, message, reason, null);
4242
}
4343

44-
public DatastoreException(int code, String message, String reason) {
45-
super(code, message, reason, true);
44+
public DatastoreException(int code, String message, String reason, Throwable cause) {
45+
super(code, message, reason, true, cause);
4646
}
4747

4848
public DatastoreException(IOException exception) {
@@ -63,7 +63,7 @@ protected Set<Error> retryableErrors() {
6363
*/
6464
static DatastoreException translateAndThrow(RetryHelperException ex) {
6565
BaseServiceException.translateAndPropagateIfPossible(ex);
66-
throw new DatastoreException(UNKNOWN_CODE, ex.getMessage(), null);
66+
throw new DatastoreException(UNKNOWN_CODE, ex.getMessage(), null, ex.getCause());
6767
}
6868

6969
/**

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ public void testDatastoreException() throws Exception {
7272
assertNull(exception.getMessage());
7373
assertTrue(exception.retryable());
7474
assertTrue(exception.idempotent());
75-
7675
}
7776

7877
@Test
7978
public void testTranslateAndThrow() throws Exception {
80-
DatastoreException cause = new DatastoreException(14, "message", "UNAVAILABLE");
79+
Exception cause = new DatastoreException(14, "message", "UNAVAILABLE");
8180
RetryHelper.RetryHelperException exceptionMock =
8281
createMock(RetryHelper.RetryHelperException.class);
8382
expect(exceptionMock.getCause()).andReturn(cause).times(2);
@@ -92,6 +91,22 @@ public void testTranslateAndThrow() throws Exception {
9291
} finally {
9392
verify(exceptionMock);
9493
}
94+
cause = new IllegalArgumentException("message");
95+
exceptionMock = createMock(RetryHelper.RetryHelperException.class);
96+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
97+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
98+
replay(exceptionMock);
99+
try {
100+
DatastoreException.translateAndThrow(exceptionMock);
101+
} catch (BaseServiceException ex) {
102+
assertEquals(DatastoreException.UNKNOWN_CODE, ex.code());
103+
assertEquals("message", ex.getMessage());
104+
assertFalse(ex.retryable());
105+
assertTrue(ex.idempotent());
106+
assertEquals(cause, ex.getCause());
107+
} finally {
108+
verify(exceptionMock);
109+
}
95110
}
96111

97112
@Test

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public DnsException(IOException exception, boolean idempotent) {
4343
super(exception, idempotent);
4444
}
4545

46-
private DnsException(int code, String message) {
47-
super(code, message, null, true);
46+
private DnsException(int code, String message, Throwable cause) {
47+
super(code, message, null, true, cause);
4848
}
4949

5050
@Override
@@ -61,6 +61,6 @@ protected Set<Error> retryableErrors() {
6161
*/
6262
static DnsException translateAndThrow(RetryHelperException ex) {
6363
BaseServiceException.translateAndPropagateIfPossible(ex);
64-
throw new DnsException(UNKNOWN_CODE, ex.getMessage());
64+
throw new DnsException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
6565
}
6666
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2016 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.cloud.dns;
18+
19+
import com.google.cloud.BaseServiceException;
20+
import com.google.cloud.RetryHelper.RetryHelperException;
21+
22+
import org.junit.Test;
23+
24+
import java.io.IOException;
25+
import java.net.SocketTimeoutException;
26+
27+
import static org.easymock.EasyMock.createMock;
28+
import static org.easymock.EasyMock.expect;
29+
import static org.easymock.EasyMock.replay;
30+
import static org.easymock.EasyMock.verify;
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertFalse;
33+
import static org.junit.Assert.assertNull;
34+
import static org.junit.Assert.assertTrue;
35+
36+
public class DnsExceptionTest {
37+
38+
@Test
39+
public void testDnsException() throws Exception {
40+
IOException cause = new SocketTimeoutException("message");
41+
DnsException exception = new DnsException(cause, true);
42+
assertEquals(DnsException.UNKNOWN_CODE, exception.code());
43+
assertNull(exception.reason());
44+
assertEquals("message", exception.getMessage());
45+
assertEquals(cause, exception.getCause());
46+
assertTrue(exception.retryable());
47+
assertTrue(exception.idempotent());
48+
}
49+
50+
@Test
51+
public void testTranslateAndThrow() throws Exception {
52+
IOException timeoutException = new SocketTimeoutException("message");
53+
Exception cause = new DnsException(timeoutException, true);
54+
RetryHelperException exceptionMock = createMock(RetryHelperException.class);
55+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
56+
replay(exceptionMock);
57+
try {
58+
DnsException.translateAndThrow(exceptionMock);
59+
} catch (BaseServiceException ex) {
60+
assertEquals(DnsException.UNKNOWN_CODE, ex.code());
61+
assertNull(ex.reason());
62+
assertEquals("message", ex.getMessage());
63+
assertEquals(timeoutException, ex.getCause());
64+
assertTrue(ex.retryable());
65+
assertTrue(ex.idempotent());
66+
} finally {
67+
verify(exceptionMock);
68+
}
69+
cause = new IllegalArgumentException("message");
70+
exceptionMock = createMock(RetryHelperException.class);
71+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
72+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
73+
replay(exceptionMock);
74+
try {
75+
DnsException.translateAndThrow(exceptionMock);
76+
} catch (BaseServiceException ex) {
77+
assertEquals(DnsException.UNKNOWN_CODE, ex.code());
78+
assertEquals("message", ex.getMessage());
79+
assertFalse(ex.retryable());
80+
assertTrue(ex.idempotent());
81+
assertEquals(cause, ex.getCause());
82+
} finally {
83+
verify(exceptionMock);
84+
}
85+
}
86+
}

gcloud-java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerException.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public class ResourceManagerException extends BaseServiceException {
4848
private static final long serialVersionUID = -9207194488966554136L;
4949

5050
public ResourceManagerException(int code, String message) {
51-
super(code, message, null, true);
51+
this(code, message, null);
52+
}
53+
54+
private ResourceManagerException(int code, String message, Throwable cause) {
55+
super(code, message, null, true, cause);
5256
}
5357

5458
public ResourceManagerException(IOException exception) {
@@ -70,6 +74,6 @@ protected Set<Error> retryableErrors() {
7074
*/
7175
static ResourceManagerException translateAndThrow(RetryHelperException ex) {
7276
BaseServiceException.translateAndPropagateIfPossible(ex);
73-
throw new ResourceManagerException(UNKNOWN_CODE, ex.getMessage());
77+
throw new ResourceManagerException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
7478
}
7579
}

gcloud-java-resourcemanager/src/test/java/com/google/cloud/resourcemanager/ResourceManagerExceptionTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testResourceManagerException() {
7676

7777
@Test
7878
public void testTranslateAndThrow() throws Exception {
79-
ResourceManagerException cause = new ResourceManagerException(503, "message");
79+
Exception cause = new ResourceManagerException(503, "message");
8080
RetryHelperException exceptionMock = createMock(RetryHelperException.class);
8181
expect(exceptionMock.getCause()).andReturn(cause).times(2);
8282
replay(exceptionMock);
@@ -90,5 +90,21 @@ public void testTranslateAndThrow() throws Exception {
9090
} finally {
9191
verify(exceptionMock);
9292
}
93+
cause = new IllegalArgumentException("message");
94+
exceptionMock = createMock(RetryHelperException.class);
95+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
96+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
97+
replay(exceptionMock);
98+
try {
99+
ResourceManagerException.translateAndThrow(exceptionMock);
100+
} catch (BaseServiceException ex) {
101+
assertEquals(ResourceManagerException.UNKNOWN_CODE, ex.code());
102+
assertEquals("message", ex.getMessage());
103+
assertFalse(ex.retryable());
104+
assertTrue(ex.idempotent());
105+
assertEquals(cause, ex.getCause());
106+
} finally {
107+
verify(exceptionMock);
108+
}
93109
}
94110
}

gcloud-java-storage/src/main/java/com/google/cloud/storage/StorageException.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public class StorageException extends BaseServiceException {
4646
private static final long serialVersionUID = -4168430271327813063L;
4747

4848
public StorageException(int code, String message) {
49-
super(code, message, null, true);
49+
this(code, message, null);
50+
}
51+
52+
private StorageException(int code, String message, Throwable cause) {
53+
super(code, message, null, true, cause);
5054
}
5155

5256
public StorageException(IOException exception) {
@@ -71,6 +75,6 @@ protected Set<Error> retryableErrors() {
7175
*/
7276
static StorageException translateAndThrow(RetryHelperException ex) {
7377
BaseServiceException.translateAndPropagateIfPossible(ex);
74-
throw new StorageException(UNKNOWN_CODE, ex.getMessage());
78+
throw new StorageException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
7579
}
7680
}

gcloud-java-storage/src/test/java/com/google/cloud/storage/StorageExceptionTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void testStorageException() {
107107

108108
@Test
109109
public void testTranslateAndThrow() throws Exception {
110-
StorageException cause = new StorageException(503, "message");
110+
Exception cause = new StorageException(503, "message");
111111
RetryHelperException exceptionMock = createMock(RetryHelperException.class);
112112
expect(exceptionMock.getCause()).andReturn(cause).times(2);
113113
replay(exceptionMock);
@@ -121,5 +121,21 @@ public void testTranslateAndThrow() throws Exception {
121121
} finally {
122122
verify(exceptionMock);
123123
}
124+
cause = new IllegalArgumentException("message");
125+
exceptionMock = createMock(RetryHelperException.class);
126+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
127+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
128+
replay(exceptionMock);
129+
try {
130+
StorageException.translateAndThrow(exceptionMock);
131+
} catch (BaseServiceException ex) {
132+
assertEquals(StorageException.UNKNOWN_CODE, ex.code());
133+
assertEquals("message", ex.getMessage());
134+
assertFalse(ex.retryable());
135+
assertTrue(ex.idempotent());
136+
assertEquals(cause, ex.getCause());
137+
} finally {
138+
verify(exceptionMock);
139+
}
124140
}
125141
}

0 commit comments

Comments
 (0)