Skip to content

Commit c336bee

Browse files
committed
- extract Log code from routines in GXRestAPIClient
- Fix test case when receiving unwrapped response.
1 parent dad5d59 commit c336bee

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

java/src/main/java/com/genexus/internet/GXRestAPIClient.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public < T extends IGxJSONSerializable> T getBodyGeospatial(String varName, Clas
297297
catch (Exception e) {
298298
errorCode = DESERIALIZING_ERROR_CODE;
299299
errorMessage = DESERIALIZING_ERROR_MSG;
300-
logger.error(DESERIALIZING_ERROR_MSG + " " + sdtClass, e);
300+
logError(DESERIALIZING_ERROR_CODE, DESERIALIZING_ERROR_MSG + " " + sdtClass, e);
301301
return null;
302302
}
303303
}
@@ -342,13 +342,13 @@ else if (jsonResponse.length() == 1 && jsonResponse.has(""))
342342
else {
343343
errorCode = RESPONSE_ERROR_CODE;
344344
errorMessage = RESPONSE_ERROR_MSG;
345-
logger.error(RESPONSE_ERROR_MSG );
345+
logError(RESPONSE_ERROR_CODE, RESPONSE_ERROR_MSG);
346346
}
347347
}
348348
catch( JSONException e) {
349349
errorCode = PARSING_ERROR_CODE;
350350
errorMessage = PARSING_ERROR_MSG;
351-
logger.error(PARSING_ERROR_MSG, e);
351+
logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG, e);
352352
}
353353
return jsonstr;
354354
}
@@ -381,21 +381,21 @@ else if (jsonResponse.length()>= 1) {
381381
{
382382
errorCode = RESPONSE_ERROR_CODE;
383383
errorMessage = RESPONSE_ERROR_MSG;
384-
logger.error(RESPONSE_ERROR_MSG + " " + sdtClass);
384+
logError( RESPONSE_ERROR_CODE, RESPONSE_ERROR_MSG + " " + sdtClass);
385385
return null;
386386
}
387387
}
388388
else {
389389
errorCode = RESPONSE_ERROR_CODE;
390390
errorMessage = RESPONSE_ERROR_MSG;
391-
logger.error(RESPONSE_ERROR_MSG + " " + sdtClass);
391+
logError( RESPONSE_ERROR_CODE,RESPONSE_ERROR_MSG + " " + sdtClass);
392392
return null;
393393
}
394394
}
395395
catch (json.org.json.JSONException e) {
396396
errorCode = PARSING_ERROR_CODE;
397397
errorMessage = PARSING_ERROR_MSG;
398-
logger.error(PARSING_ERROR_MSG + " " + sdtClass, e);
398+
logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG + " " + sdtClass, e);
399399
return null;
400400
}
401401
return sdt;
@@ -431,18 +431,18 @@ else if (jsonResponse.length() == 1 && jsonResponse.has(""))
431431
else {
432432
errorCode = RESPONSE_ERROR_CODE;
433433
errorMessage = RESPONSE_ERROR_MSG;
434-
logger.error(RESPONSE_ERROR_MSG + " " + elementClass);
434+
logError(RESPONSE_ERROR_CODE,RESPONSE_ERROR_MSG + " " + elementClass);
435435
}
436436
}
437437
catch (json.org.json.JSONException e) {
438438
errorCode = PARSING_ERROR_CODE;
439439
errorMessage = PARSING_ERROR_MSG;
440-
logger.error(PARSING_ERROR_MSG + " " + elementClass ,e );
440+
logError(PARSING_ERROR_CODE,PARSING_ERROR_MSG + " " + elementClass ,e );
441441
}
442442
catch (Exception e) {
443443
errorCode = DESERIALIZING_ERROR_CODE;
444444
errorMessage = DESERIALIZING_ERROR_MSG;
445-
logger.error(DESERIALIZING_ERROR_MSG + " " + elementClass, e);
445+
logError(DESERIALIZING_ERROR_CODE, DESERIALIZING_ERROR_MSG + " " + elementClass, e);
446446
}
447447
}
448448

@@ -465,7 +465,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) {
465465
catch (json.org.json.JSONException e) {
466466
errorCode = PARSING_ERROR_CODE;
467467
errorMessage = PARSING_ERROR_MSG;
468-
logger.error(PARSING_ERROR_MSG + " " + elementClasss, e);
468+
logError(PARSING_ERROR_CODE,PARSING_ERROR_MSG + " " + elementClasss, e);
469469
}
470470
return coll;
471471
}
@@ -523,14 +523,27 @@ public void RestExecute() {
523523
else {
524524
statusCode = httpClient.getStatusCode();
525525
try {
526-
jsonResponse = new JSONObject(httpClient.getString());
526+
String response = httpClient.getString();
527+
if (response.trim().startsWith("["))
528+
{
529+
// unwrapped list response
530+
response = "{\"\":" + response + "}";
531+
}
532+
jsonResponse = new JSONObject(response);
527533
}
528534
catch( JSONException e) {
529535
errorCode = PARSING_ERROR_CODE;
530536
errorMessage = PARSING_ERROR_MSG;
531-
logger.error(PARSING_ERROR_MSG, e);
537+
logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG, e);
532538
jsonResponse = new JSONObject();
533539
}
534540
}
535541
}
542+
543+
private void logError(int code, String msg) {
544+
logger.error("Error: " + Integer.toString(code) + " " + msg);
545+
}
546+
private void logError(int code, String msg, Exception e) {
547+
logger.error("Error: " + Integer.toString(code) + " " + msg, e);
548+
}
536549
}

0 commit comments

Comments
 (0)