Skip to content

Commit

Permalink
Add errorMessage to APIException
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderjordanbaker committed Dec 12, 2023
1 parent bd7985f commit ddcb5df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
34 changes: 23 additions & 11 deletions src/main/java/com/apple/itunes/storekit/client/APIException.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,52 @@
*/
public class APIException extends Exception {
private final int httpStatusCode;
private final Long apiError;
private final Long apiErrorCode;
private final String apiErrorMessage;

public APIException(int httpStatusCode) {
super("Failed to call API with httpStatusCode=" + httpStatusCode);
this.httpStatusCode = httpStatusCode;
this.apiError = null;
this.apiErrorCode = null;
this.apiErrorMessage = null;
}

public APIException(int httpStatusCode, APIError apiError) {
public APIException(int httpStatusCode, APIError apiError, String apiErrorMessage) {
super("Failed to call API with error=\"" + apiErrorMessage + "\"");
this.httpStatusCode = httpStatusCode;
this.apiError = apiError != null ? apiError.errorCode() : null;
this.apiErrorCode = apiError != null ? apiError.errorCode() : null;
this.apiErrorMessage = apiErrorMessage;
}

public APIException(int httpStatusCode, Long rawApiError) {
public APIException(int httpStatusCode, Long rawApiError, String apiErrorMessage) {
super("Failed to call API with error=\"" + apiErrorMessage + "\"");
this.httpStatusCode = httpStatusCode;
this.apiError = rawApiError;
this.apiErrorCode = rawApiError;
this.apiErrorMessage = apiErrorMessage;
}

public int getHttpStatusCode() {
return httpStatusCode;
}

public APIError getApiError() {
return apiError != null ? APIError.fetchErrorResponseFromErrorCode(apiError) : null;
public APIError getApiErrorCode() {
return apiErrorCode != null ? APIError.fetchErrorResponseFromErrorCode(apiErrorCode) : null;
}

public Long getRawApiError() {
return apiError;
return apiErrorCode;
}

public String getApiErrorMessage() {
return apiErrorMessage;
}

@Override
public String toString() {
return "APIException{" +
"httpStatusCode=" + httpStatusCode +
", apiError=" + apiError +
"} " + super.toString();
", apiErrorCode=" + apiErrorCode +
", apiErrorMessage='" + apiErrorMessage + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected <T> T makeHttpCall(String path, String method, Map<String, List<String
ResponseBody responseBody = r.body();
if (responseBody != null) {
ErrorPayload errorPayload = objectMapper.readValue(responseBody.charStream(), ErrorPayload.class);
throw new APIException(r.code(), errorPayload.getErrorCode());
throw new APIException(r.code(), errorPayload.getErrorCode(), errorPayload.getErrorMessage());
}
} catch (APIException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,9 @@ public void testAPIError() throws IOException {
client.getTransactionInfo("1234");
} catch (APIException e) {
Assertions.assertEquals(500, e.getHttpStatusCode());
Assertions.assertEquals(APIError.GENERAL_INTERNAL, e.getApiError());
Assertions.assertEquals(APIError.GENERAL_INTERNAL, e.getApiErrorCode());
Assertions.assertEquals(5000000L, e.getRawApiError());
Assertions.assertEquals("An unknown error occurred.", e.getApiErrorMessage());
return;
}
Assertions.fail();
Expand All @@ -488,8 +489,9 @@ public void testAPITooManyRequests() throws IOException {
client.getTransactionInfo("1234");
} catch (APIException e) {
Assertions.assertEquals(429, e.getHttpStatusCode());
Assertions.assertEquals(APIError.RATE_LIMIT_EXCEEDED, e.getApiError());
Assertions.assertEquals(APIError.RATE_LIMIT_EXCEEDED, e.getApiErrorCode());
Assertions.assertEquals(4290000L, e.getRawApiError());
Assertions.assertEquals("Rate limit exceeded.", e.getApiErrorMessage());
return;
}
Assertions.fail();
Expand All @@ -503,8 +505,9 @@ public void testAPIUnknownError() throws IOException {
client.getTransactionInfo("1234");
} catch (APIException e) {
Assertions.assertEquals(400, e.getHttpStatusCode());
Assertions.assertNull(e.getApiError());
Assertions.assertNull(e.getApiErrorCode());
Assertions.assertEquals(9990000L, e.getRawApiError());
Assertions.assertEquals("Testing error.", e.getApiErrorMessage());
return;
}
Assertions.fail();
Expand Down

0 comments on commit ddcb5df

Please sign in to comment.