From 78b171b451989b5366c578c393d0bfa4ee127573 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 8 Oct 2024 17:48:21 +0530 Subject: [PATCH 1/2] chore: github issues resolved --- .../contentstack/sdk/CSHttpConnection.java | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/contentstack/sdk/CSHttpConnection.java b/src/main/java/com/contentstack/sdk/CSHttpConnection.java index 9eda41c0..95ea130b 100644 --- a/src/main/java/com/contentstack/sdk/CSHttpConnection.java +++ b/src/main/java/com/contentstack/sdk/CSHttpConnection.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.net.SocketTimeoutException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Iterator; @@ -202,22 +203,35 @@ private void getService(String requestUrl) throws IOException { requestUrl = request.url().toString(); } - Response response = this.service.getRequest(requestUrl, this.headers).execute(); - if (response.isSuccessful()) { - assert response.body() != null; - if (request != null) { - response = pluginResponseImp(request, response); - } - responseJSON = new JSONObject(response.body().string()); - if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) { - handleJSONArray(); + try { + Response response = this.service.getRequest(requestUrl, this.headers).execute(); + if (response.isSuccessful()) { + assert response.body() != null; + if (request != null) { + response = pluginResponseImp(request, response); + } + String responseBody = response.body().string(); + try { + responseJSON = new JSONObject(responseBody); + if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) { + handleJSONArray(); + } + connectionRequest.onRequestFinished(CSHttpConnection.this); + } catch (JSONException e) { + // Handle non-JSON response + setError("Invalid JSON response: " + responseBody); + } + } else { + assert response.errorBody() != null; + setError(response.errorBody().string()); } - connectionRequest.onRequestFinished(CSHttpConnection.this); - } else { - assert response.errorBody() != null; - setError(response.errorBody().string()); + } catch (SocketTimeoutException e) { + // Handle timeout + setError("Request timed out: " + e.getMessage()); + } catch (IOException e) { + // Handle other IO exceptions + setError("IO error occurred: " + e.getMessage()); } - } private Request pluginRequestImp(String requestUrl) { @@ -261,7 +275,14 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) { } void setError(String errResp) { - responseJSON = new JSONObject(errResp); // Parse error string to JSONObject + try { + responseJSON = new JSONObject(errResp); + } catch (JSONException e) { + // If errResp is not valid JSON, create a new JSONObject with the error message + responseJSON = new JSONObject(); + responseJSON.put(ERROR_MESSAGE, errResp); + responseJSON.put(ERROR_CODE, "unknown"); + } responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE)); responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE)); responseJSON.put(ERRORS, responseJSON.optString(ERRORS)); From 8a6b9d14311f132c27dbb6baa1898753b7d22dbd Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 8 Oct 2024 18:44:28 +0530 Subject: [PATCH 2/2] chore: removed error code part --- src/main/java/com/contentstack/sdk/CSHttpConnection.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/contentstack/sdk/CSHttpConnection.java b/src/main/java/com/contentstack/sdk/CSHttpConnection.java index 95ea130b..e3c44d65 100644 --- a/src/main/java/com/contentstack/sdk/CSHttpConnection.java +++ b/src/main/java/com/contentstack/sdk/CSHttpConnection.java @@ -281,7 +281,6 @@ void setError(String errResp) { // If errResp is not valid JSON, create a new JSONObject with the error message responseJSON = new JSONObject(); responseJSON.put(ERROR_MESSAGE, errResp); - responseJSON.put(ERROR_CODE, "unknown"); } responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE)); responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));