Skip to content

Commit

Permalink
Merge pull request #119 from alexanderjordanbaker/ResolveEmptyUnexpec…
Browse files Browse the repository at this point in the history
…tedBodiesInErrorCases

Handle parsing empty or unexpected bodies without throwing a caused-b…
  • Loading branch information
alexanderjordanbaker authored Jul 16, 2024
2 parents 49d761b + 36045c1 commit 1b9f670
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit 1b9f670

Please sign in to comment.