Skip to content

Commit b7d773b

Browse files
committed
suppress runtime exception upon non-status error responses
1 parent 45d246d commit b7d773b

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

util/src/main/java/io/kubernetes/client/util/generic/GenericKubernetesApi.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,12 @@ private <DataType extends KubernetesType> KubernetesApiResponse<DataType> execut
736736
try {
737737
status = apiClient.getJSON().deserialize(e.getResponseBody(), V1Status.class);
738738
} catch (JsonSyntaxException jsonEx) {
739-
throw new RuntimeException(jsonEx);
739+
// craft a status object
740+
return new KubernetesApiResponse<>(
741+
new V1Status().code(e.getCode()).message(e.getResponseBody()), e.getCode());
740742
}
741743
if (null == status) { // the response body can be something unexpected sometimes..
744+
// this line should never reach?
742745
throw new RuntimeException(e);
743746
}
744747
return new KubernetesApiResponse<>(status, e.getCode());

util/src/test/java/io/kubernetes/client/util/generic/KubernetesApiResponseTest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
1717
import static com.github.tomakehurst.wiremock.client.WireMock.get;
1818
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
19-
import static org.junit.Assert.assertNull;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.junit.Assert.fail;
19+
import static org.junit.Assert.*;
2220

2321
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2422
import com.google.gson.Gson;
25-
import com.google.gson.JsonSyntaxException;
2623
import io.kubernetes.client.openapi.ApiClient;
2724
import io.kubernetes.client.openapi.ApiException;
2825
import io.kubernetes.client.openapi.models.*;
@@ -65,15 +62,12 @@ public void testErrorStatusHandler() throws ApiException {
6562

6663
@Test
6764
public void testNotDeserializableResponse() {
65+
String message = "-foobar";
6866
wireMockRule.stubFor(
6967
get(urlEqualTo("/api/v1/namespaces/default/pods/foo"))
70-
.willReturn(aResponse().withStatus(403).withBody("-foobar")));
71-
try {
72-
podClient.get("default", "foo");
73-
} catch (RuntimeException e) {
74-
assertTrue(JsonSyntaxException.class.equals(e.getCause().getClass()));
75-
return;
76-
}
77-
fail("no exception thrown");
68+
.willReturn(aResponse().withStatus(403).withBody(message)));
69+
KubernetesApiResponse response = podClient.get("default", "foo");
70+
assertFalse(response.isSuccess());
71+
assertEquals(response.getStatus().getMessage(), message);
7872
}
7973
}

0 commit comments

Comments
 (0)