Skip to content

Commit

Permalink
Merge pull request #274 from mwarzybok-sumoheavy/feature/SP-874
Browse files Browse the repository at this point in the history
SP-874 Support "errors" array
  • Loading branch information
bobbrodie committed Feb 26, 2024
2 parents 2b906dc + e1aedcd commit 9dcb96f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
56 changes: 55 additions & 1 deletion src/main/java/com/bitpay/sdk/client/ResponseParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ public static String getJsonDataFromJsonResponse(final String jsonResponse) thro
);
}

node = rootNode.get("errors");
if (node != null) {
String message = "";

if (node.isArray()) {
for (final JsonNode errorNode : node) {
message += "\n" + errorNode.asText();
}

BitPayExceptionProvider.throwApiExceptionWithMessage(
rootNode.get("message").textValue(),
getCode(rootNode)
);
}
}


if ("success".equals(status)) {
node = rootNode.get("data");

Expand All @@ -50,7 +67,7 @@ public static String getJsonDataFromJsonResponse(final String jsonResponse) thro
}
}

handleError(rootNode);
handleErrors(rootNode);

node = rootNode.get("data");
if (node != null) {
Expand All @@ -65,6 +82,43 @@ public static String getJsonDataFromJsonResponse(final String jsonResponse) thro
return jsonString;
}

private static void handleErrors(JsonNode rootNode) throws BitPayApiException {

handleError(rootNode);

JsonNode errors = rootNode.get("errors");
if (errors == null) {
return;
}

StringBuilder finalMessage = new StringBuilder();

for (JsonNode errorNode : errors) {
String errorValue = errorNode.has("error") ? errorNode.get("error").textValue() : "";
String paramValue = errorNode.has("param") ? errorNode.get("param").textValue() : "";

if (errorValue != null && errorValue.endsWith(".")) {
errorValue = errorValue.substring(0, errorValue.length() - 1);
}

String message;
message = errorValue + " " + paramValue;
message = message.trim();

if (!message.endsWith(".")) {
message += ".";
}

if (!finalMessage.toString().equals("")) {
message = " " + message;
}

finalMessage.append(message);
}

BitPayExceptionProvider.throwApiExceptionWithMessage(finalMessage.toString(), getCode(rootNode));
}

private static void handleError(JsonNode rootNode) throws BitPayApiException {
if (!rootNode.has("error")) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/bitpay/sdk/client/ResponseParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void it_should_throws_bitpay_api_exception_for_response_with_errors() {
BitPayApiException.class,
() -> {
// given
final String responseContent = "{\"code\":\"500\",\"status\":\"error\",\"data\":{},\"message\":\"Error message text\"}";
final String responseContent = "{\"errors\":[{\"error\":\"Missing required parameter.\",\"param\":\"price\"},{\"error\":\"Missing required parameter.\",\"param\":\"currency\"}]}";

// when
String result = ResponseParser.getJsonDataFromJsonResponse(responseContent);
Expand All @@ -81,6 +81,6 @@ public void it_should_throws_bitpay_api_exception_for_response_with_errors() {
Assertions.assertEquals(responseContent, result);
}
);
Assertions.assertEquals("Error message text", exception.getMessage());
Assertions.assertEquals("Missing required parameter price. Missing required parameter currency.", exception.getMessage());
}
}

0 comments on commit 9dcb96f

Please sign in to comment.