Skip to content

Commit 6c726db

Browse files
author
rachinkapoor
committed
Request Client should not throw exception with empty response body.
1 parent f263f26 commit 6c726db

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/main/java/com/ost/lib/OSTRequestClient.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class OSTRequestClient {
2828
private String apiKey;
2929
private String apiSecret;
3030
private String apiEndpoint;
31-
private final Gson gson = new Gson();
31+
private static final Gson gson = new Gson();
3232
private OkHttpClient client;
3333
private static final Escaper FormParameterEscaper = UrlEscapers.urlFormParameterEscaper();
3434
private static final Escaper PathSegmentEscaper = UrlEscapers.urlPathSegmentEscaper();
@@ -234,14 +234,17 @@ private String signQueryParams( Buffer hmacInputBuffer ) {
234234
return signature;
235235
}
236236

237+
private static String SOMETHING_WRONG_RESPONSE = "{'success': false, 'err': {'code': 'SOMETHING_WENT_WRONG', 'internal_id': 'SDK(SOMETHING_WENT_WRONG)', 'msg': '', 'error_data':[]}}";
237238
private static String getResponseBodyAsString( okhttp3.Response response ) {
238239
// Process the response.
239240
String responseBody;
240241
if ( response.body() != null ) {
241242
try {
242243
responseBody = response.body().string();
243-
if ( DEBUG ) System.out.println("responseBody:\n" + responseBody + "\n");
244-
return responseBody;
244+
if ( responseBody.length() > 0 ) {
245+
if ( DEBUG ) System.out.println("responseBody:\n" + responseBody + "\n");
246+
return responseBody;
247+
}
245248
} catch (IOException e) {
246249
// Silently handle the error.
247250
e.printStackTrace();
@@ -266,14 +269,20 @@ private static String getResponseBodyAsString( okhttp3.Response response ) {
266269
responseBody = "{'success': false, 'err': {'code': 'GATEWAY_TIMEOUT', 'internal_id': 'SDK(GATEWAY_TIMEOUT)', 'msg': '', 'error_data':[]}}";
267270
break;
268271
default:
269-
responseBody = "{'success': false, 'err': {'code': 'SOMETHING_WENT_WRONG', 'internal_id': 'SDK(SOMETHING_WENT_WRONG)', 'msg': '', 'error_data':[]}}";
272+
responseBody = SOMETHING_WRONG_RESPONSE;
270273
}
271274

272275
if ( DEBUG ) System.out.println("local responseBody:\n" + responseBody + "\n");
273276
return responseBody;
274277
}
275278

276-
private JsonObject buildApiResponse(String jsonString ) {
277-
return gson.fromJson( jsonString , JsonObject.class);
279+
private static JsonObject buildApiResponse(String jsonString ) {
280+
try {
281+
return gson.fromJson( jsonString , JsonObject.class);
282+
} catch (Exception e) {
283+
e.printStackTrace();
284+
}
285+
if ( DEBUG ) System.out.println("Failed to parse response. local responseBody:\n" + SOMETHING_WRONG_RESPONSE + "\n");
286+
return gson.fromJson(SOMETHING_WRONG_RESPONSE, JsonObject.class);
278287
}
279288
}

0 commit comments

Comments
 (0)