Skip to content

Commit b0446ba

Browse files
committed
Chain cause in ComputeException.translateAndThrow, add tests
1 parent a05edf3 commit b0446ba

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeException.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public class ComputeException extends BaseServiceException {
3333
private static final long serialVersionUID = -8039359778707845810L;
3434

3535
public ComputeException(int code, String message) {
36-
super(code, message, null, true);
36+
super(code, message, null, true, null);
37+
}
38+
39+
private ComputeException(int code, String message, Throwable cause) {
40+
super(code, message, null, true, cause);
3741
}
3842

3943
public ComputeException(IOException exception) {
@@ -54,6 +58,6 @@ protected Set<Error> retryableErrors() {
5458
*/
5559
static BaseServiceException translateAndThrow(RetryHelperException ex) {
5660
BaseServiceException.translateAndPropagateIfPossible(ex);
57-
throw new ComputeException(UNKNOWN_CODE, ex.getMessage());
61+
throw new ComputeException(UNKNOWN_CODE, ex.getMessage(), ex.getCause());
5862
}
5963
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.gcloud.compute;
18+
19+
import static org.easymock.EasyMock.createMock;
20+
import static org.easymock.EasyMock.expect;
21+
import static org.easymock.EasyMock.replay;
22+
import static org.easymock.EasyMock.verify;
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertNull;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import com.google.gcloud.BaseServiceException;
29+
import com.google.gcloud.RetryHelper.RetryHelperException;
30+
31+
import org.junit.Test;
32+
33+
import java.io.IOException;
34+
import java.net.SocketTimeoutException;
35+
36+
public class ComputeExceptionTest {
37+
38+
@Test
39+
public void testResourceManagerException() {
40+
ComputeException exception = new ComputeException(500, "message");
41+
assertEquals(500, exception.code());
42+
assertEquals("message", exception.getMessage());
43+
assertNull(exception.reason());
44+
assertTrue(exception.retryable());
45+
assertTrue(exception.idempotent());
46+
47+
exception = new ComputeException(403, "message");
48+
assertEquals(403, exception.code());
49+
assertEquals("message", exception.getMessage());
50+
assertNull(exception.reason());
51+
assertFalse(exception.retryable());
52+
assertTrue(exception.idempotent());
53+
54+
IOException cause = new SocketTimeoutException();
55+
exception = new ComputeException(cause);
56+
assertNull(exception.reason());
57+
assertNull(exception.getMessage());
58+
assertTrue(exception.retryable());
59+
assertTrue(exception.idempotent());
60+
assertEquals(cause, exception.getCause());
61+
}
62+
63+
@Test
64+
public void testTranslateAndThrow() throws Exception {
65+
Exception cause = new ComputeException(500, "message");
66+
RetryHelperException exceptionMock = createMock(RetryHelperException.class);
67+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
68+
replay(exceptionMock);
69+
try {
70+
ComputeException.translateAndThrow(exceptionMock);
71+
} catch (BaseServiceException ex) {
72+
assertEquals(500, ex.code());
73+
assertEquals("message", ex.getMessage());
74+
assertTrue(ex.retryable());
75+
assertTrue(ex.idempotent());
76+
} finally {
77+
verify(exceptionMock);
78+
}
79+
cause = new IllegalArgumentException("message");
80+
exceptionMock = createMock(RetryHelperException.class);
81+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
82+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
83+
replay(exceptionMock);
84+
try {
85+
ComputeException.translateAndThrow(exceptionMock);
86+
} catch (BaseServiceException ex) {
87+
assertEquals(ComputeException.UNKNOWN_CODE, ex.code());
88+
assertEquals("message", ex.getMessage());
89+
assertFalse(ex.retryable());
90+
assertTrue(ex.idempotent());
91+
assertEquals(cause, ex.getCause());
92+
} finally {
93+
verify(exceptionMock);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)