Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle parsing empty or unexpected bodies without throwing a caused-b… #119

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ protected <T> T makeHttpCall(String path, String method, Map<String, List<String
try {
var responseBody = r.body();
if (responseBody != null) {
ErrorPayload errorPayload = objectMapper.readValue(responseBody, ErrorPayload.class);
ErrorPayload errorPayload;
try {
errorPayload = objectMapper.readValue(responseBody, ErrorPayload.class);
} catch (JsonProcessingException ignored) {
// If we cannot parse the body, then simply return the status code
throw new APIException(r.statusCode());
}
throw new APIException(r.statusCode(), errorPayload.getErrorCode(), errorPayload.getErrorMessage());
}
} catch (APIException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,23 @@ public void testAPIUnknownError() throws IOException {
Assertions.fail();
}

@Test
public void testExceptionWithMalformedJson() throws IOException {
String body = "<html><body>I'm not a JSON error</body></html>";
AppStoreServerAPIClient client = getAppStoreServerAPIClient(body, request -> {}, 400);
try {
client.getTransactionInfo("1234");
} catch (APIException e) {
Assertions.assertEquals(400, e.getHttpStatusCode());
Assertions.assertNull(e.getApiError());
Assertions.assertNull(e.getRawApiError());
Assertions.assertNull(e.getApiErrorMessage());
Assertions.assertNull(e.getCause());
return;
}
Assertions.fail();
}

@Test
public void testDecodingWithUnknownEnumValue() throws IOException, APIException {
String body = TestingUtility.readFile("models/transactionHistoryResponseWithMalformedEnvironment.json");
Expand Down
Loading