Skip to content

Commit 2643243

Browse files
mziccardaozarov
authored andcommitted
Chain cause in ComputeException.translateAndThrow, add tests (#948)
Chain cause in ComputeException.translateAndThrow, add tests and make ComputeException constructors package scoped.
1 parent a05edf3 commit 2643243

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ public class ComputeException extends BaseServiceException {
3232
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(new Error(500, null));
3333
private static final long serialVersionUID = -8039359778707845810L;
3434

35-
public ComputeException(int code, String message) {
36-
super(code, message, null, true);
35+
ComputeException(int code, String message) {
36+
super(code, message, null, true, null);
37+
}
38+
39+
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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.assertSame;
27+
import static org.junit.Assert.assertTrue;
28+
29+
import com.google.gcloud.BaseServiceException;
30+
import com.google.gcloud.RetryHelper.RetryHelperException;
31+
32+
import org.junit.Test;
33+
34+
import java.io.IOException;
35+
import java.net.SocketTimeoutException;
36+
37+
public class ComputeExceptionTest {
38+
39+
@Test
40+
public void testResourceManagerException() {
41+
ComputeException exception = new ComputeException(500, "message");
42+
assertEquals(500, exception.code());
43+
assertEquals("message", exception.getMessage());
44+
assertNull(exception.reason());
45+
assertTrue(exception.retryable());
46+
assertTrue(exception.idempotent());
47+
48+
exception = new ComputeException(403, "message");
49+
assertEquals(403, exception.code());
50+
assertEquals("message", exception.getMessage());
51+
assertNull(exception.reason());
52+
assertFalse(exception.retryable());
53+
assertTrue(exception.idempotent());
54+
55+
IOException cause = new SocketTimeoutException();
56+
exception = new ComputeException(cause);
57+
assertNull(exception.reason());
58+
assertNull(exception.getMessage());
59+
assertTrue(exception.retryable());
60+
assertTrue(exception.idempotent());
61+
assertSame(cause, exception.getCause());
62+
63+
exception = new ComputeException(403, "message", cause);
64+
assertEquals(403, exception.code());
65+
assertEquals("message", exception.getMessage());
66+
assertNull(exception.reason());
67+
assertFalse(exception.retryable());
68+
assertTrue(exception.idempotent());
69+
assertSame(cause, exception.getCause());
70+
}
71+
72+
@Test
73+
public void testTranslateAndThrow() throws Exception {
74+
Exception cause = new ComputeException(500, "message");
75+
RetryHelperException exceptionMock = createMock(RetryHelperException.class);
76+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
77+
replay(exceptionMock);
78+
try {
79+
ComputeException.translateAndThrow(exceptionMock);
80+
} catch (BaseServiceException ex) {
81+
assertEquals(500, ex.code());
82+
assertEquals("message", ex.getMessage());
83+
assertTrue(ex.retryable());
84+
assertTrue(ex.idempotent());
85+
} finally {
86+
verify(exceptionMock);
87+
}
88+
cause = new IllegalArgumentException("message");
89+
exceptionMock = createMock(RetryHelperException.class);
90+
expect(exceptionMock.getMessage()).andReturn("message").times(1);
91+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
92+
replay(exceptionMock);
93+
try {
94+
ComputeException.translateAndThrow(exceptionMock);
95+
} catch (BaseServiceException ex) {
96+
assertEquals(ComputeException.UNKNOWN_CODE, ex.code());
97+
assertEquals("message", ex.getMessage());
98+
assertFalse(ex.retryable());
99+
assertTrue(ex.idempotent());
100+
assertSame(cause, ex.getCause());
101+
} finally {
102+
verify(exceptionMock);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)