Skip to content

Commit 8780711

Browse files
authored
fix(exception): better handle non-JSON response bodies (#1404)
* fix(exception): better handle non-JSON response bodies * unwanted diff * formatting * test
1 parent 0230951 commit 8780711

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Diff for: src/main/java/com/adyen/service/resource/Resource.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.adyen.model.ApiError;
3030
import com.adyen.model.RequestOptions;
3131
import com.adyen.service.exception.ApiException;
32+
import com.fasterxml.jackson.core.JacksonException;
33+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
3234
import com.google.gson.Gson;
3335

3436
import java.io.IOException;
@@ -113,7 +115,11 @@ public String request(String json, RequestOptions requestOptions, ApiConstants.H
113115
} catch (HTTPClientException e) {
114116
apiException = new ApiException(e.getMessage(), e.getCode(), e.getResponseHeaders());
115117
apiException.setResponseBody(e.getResponseBody());
116-
apiException.setError(ApiError.fromJson(e.getResponseBody()));
118+
try {
119+
apiException.setError(ApiError.fromJson(e.getResponseBody()));
120+
} catch (Exception ignore) {
121+
// Response body could not be parsed (e.g. not JSON), raw response body available in ApiException#responseBody
122+
}
117123
}
118124
throw apiException;
119125
}

Diff for: src/test/java/com/adyen/service/ResourceTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.util.Map;
4141

4242
import static org.junit.Assert.*;
43+
import static org.mockito.ArgumentMatchers.any;
44+
import static org.mockito.ArgumentMatchers.anyBoolean;
4345
import static org.mockito.Mockito.verify;
4446
import static org.mockito.Mockito.when;
4547

@@ -82,6 +84,20 @@ public void testRequestQueryString() throws Exception {
8284
verify(clientInterfaceMock).request("/companies/adyen/merchants", null, null, false, null, ApiConstants.HttpMethod.GET, queryString);
8385
}
8486

87+
@Test
88+
public void testNonJsonError() throws Exception {
89+
Map<String, String> pathParams = Collections.singletonMap("companyId", "adyen");
90+
Map<String, String> queryString = Collections.singletonMap("pageSize", "10");
91+
Resource resource = new Resource(serviceMock, "/companies/{companyId}/merchants", null);
92+
93+
HTTPClientException error = new HTTPClientException(500, "error", Collections.emptyMap(), "not JSON");
94+
when(clientInterfaceMock.request(any(), any(), any(), anyBoolean(), any(), any(), any())).thenThrow(error);
95+
96+
ApiException thrown = assertThrows(ApiException.class, () -> resource.request(null, null, ApiConstants.HttpMethod.GET, pathParams, queryString));
97+
assertEquals("not JSON", thrown.getResponseBody());
98+
assertNull(thrown.getError());
99+
}
100+
85101
@Test
86102
public void testRequestExceptionEmpty() throws IOException, HTTPClientException {
87103
try {

0 commit comments

Comments
 (0)