@@ -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 ;
0 commit comments