Skip to content

Commit

Permalink
Merge pull request #139 from contentstack/fix/DX-1412-SRE-issues
Browse files Browse the repository at this point in the history
chore: github issues resolved
  • Loading branch information
reeshika-h authored Oct 9, 2024
2 parents ef29655 + 8a6b9d1 commit 9bf81c6
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -202,22 +203,35 @@ private void getService(String requestUrl) throws IOException {
requestUrl = request.url().toString();
}

Response<ResponseBody> 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<ResponseBody> 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) {
Expand Down Expand Up @@ -261,7 +275,13 @@ 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_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));
Expand Down

0 comments on commit 9bf81c6

Please sign in to comment.