Skip to content

Commit 9dcb96f

Browse files
authored
Merge pull request #274 from mwarzybok-sumoheavy/feature/SP-874
SP-874 Support "errors" array
2 parents 2b906dc + e1aedcd commit 9dcb96f

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/main/java/com/bitpay/sdk/client/ResponseParser.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ public static String getJsonDataFromJsonResponse(final String jsonResponse) thro
4141
);
4242
}
4343

44+
node = rootNode.get("errors");
45+
if (node != null) {
46+
String message = "";
47+
48+
if (node.isArray()) {
49+
for (final JsonNode errorNode : node) {
50+
message += "\n" + errorNode.asText();
51+
}
52+
53+
BitPayExceptionProvider.throwApiExceptionWithMessage(
54+
rootNode.get("message").textValue(),
55+
getCode(rootNode)
56+
);
57+
}
58+
}
59+
60+
4461
if ("success".equals(status)) {
4562
node = rootNode.get("data");
4663

@@ -50,7 +67,7 @@ public static String getJsonDataFromJsonResponse(final String jsonResponse) thro
5067
}
5168
}
5269

53-
handleError(rootNode);
70+
handleErrors(rootNode);
5471

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

85+
private static void handleErrors(JsonNode rootNode) throws BitPayApiException {
86+
87+
handleError(rootNode);
88+
89+
JsonNode errors = rootNode.get("errors");
90+
if (errors == null) {
91+
return;
92+
}
93+
94+
StringBuilder finalMessage = new StringBuilder();
95+
96+
for (JsonNode errorNode : errors) {
97+
String errorValue = errorNode.has("error") ? errorNode.get("error").textValue() : "";
98+
String paramValue = errorNode.has("param") ? errorNode.get("param").textValue() : "";
99+
100+
if (errorValue != null && errorValue.endsWith(".")) {
101+
errorValue = errorValue.substring(0, errorValue.length() - 1);
102+
}
103+
104+
String message;
105+
message = errorValue + " " + paramValue;
106+
message = message.trim();
107+
108+
if (!message.endsWith(".")) {
109+
message += ".";
110+
}
111+
112+
if (!finalMessage.toString().equals("")) {
113+
message = " " + message;
114+
}
115+
116+
finalMessage.append(message);
117+
}
118+
119+
BitPayExceptionProvider.throwApiExceptionWithMessage(finalMessage.toString(), getCode(rootNode));
120+
}
121+
68122
private static void handleError(JsonNode rootNode) throws BitPayApiException {
69123
if (!rootNode.has("error")) {
70124
return;

src/test/java/com/bitpay/sdk/client/ResponseParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void it_should_throws_bitpay_api_exception_for_response_with_errors() {
7272
BitPayApiException.class,
7373
() -> {
7474
// given
75-
final String responseContent = "{\"code\":\"500\",\"status\":\"error\",\"data\":{},\"message\":\"Error message text\"}";
75+
final String responseContent = "{\"errors\":[{\"error\":\"Missing required parameter.\",\"param\":\"price\"},{\"error\":\"Missing required parameter.\",\"param\":\"currency\"}]}";
7676

7777
// when
7878
String result = ResponseParser.getJsonDataFromJsonResponse(responseContent);
@@ -81,6 +81,6 @@ public void it_should_throws_bitpay_api_exception_for_response_with_errors() {
8181
Assertions.assertEquals(responseContent, result);
8282
}
8383
);
84-
Assertions.assertEquals("Error message text", exception.getMessage());
84+
Assertions.assertEquals("Missing required parameter price. Missing required parameter currency.", exception.getMessage());
8585
}
8686
}

0 commit comments

Comments
 (0)