Skip to content

Commit 9bf81c6

Browse files
authored
Merge pull request #139 from contentstack/fix/DX-1412-SRE-issues
chore: github issues resolved
2 parents ef29655 + 8a6b9d1 commit 9bf81c6

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.IOException;
1212
import java.io.UnsupportedEncodingException;
1313
import java.net.URLEncoder;
14+
import java.net.SocketTimeoutException;
1415
import java.nio.charset.StandardCharsets;
1516
import java.util.HashMap;
1617
import java.util.Iterator;
@@ -202,22 +203,35 @@ private void getService(String requestUrl) throws IOException {
202203
requestUrl = request.url().toString();
203204
}
204205

205-
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
206-
if (response.isSuccessful()) {
207-
assert response.body() != null;
208-
if (request != null) {
209-
response = pluginResponseImp(request, response);
210-
}
211-
responseJSON = new JSONObject(response.body().string());
212-
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
213-
handleJSONArray();
206+
try {
207+
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
208+
if (response.isSuccessful()) {
209+
assert response.body() != null;
210+
if (request != null) {
211+
response = pluginResponseImp(request, response);
212+
}
213+
String responseBody = response.body().string();
214+
try {
215+
responseJSON = new JSONObject(responseBody);
216+
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
217+
handleJSONArray();
218+
}
219+
connectionRequest.onRequestFinished(CSHttpConnection.this);
220+
} catch (JSONException e) {
221+
// Handle non-JSON response
222+
setError("Invalid JSON response: " + responseBody);
223+
}
224+
} else {
225+
assert response.errorBody() != null;
226+
setError(response.errorBody().string());
214227
}
215-
connectionRequest.onRequestFinished(CSHttpConnection.this);
216-
} else {
217-
assert response.errorBody() != null;
218-
setError(response.errorBody().string());
228+
} catch (SocketTimeoutException e) {
229+
// Handle timeout
230+
setError("Request timed out: " + e.getMessage());
231+
} catch (IOException e) {
232+
// Handle other IO exceptions
233+
setError("IO error occurred: " + e.getMessage());
219234
}
220-
221235
}
222236

223237
private Request pluginRequestImp(String requestUrl) {
@@ -261,7 +275,13 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
261275
}
262276

263277
void setError(String errResp) {
264-
responseJSON = new JSONObject(errResp); // Parse error string to JSONObject
278+
try {
279+
responseJSON = new JSONObject(errResp);
280+
} catch (JSONException e) {
281+
// If errResp is not valid JSON, create a new JSONObject with the error message
282+
responseJSON = new JSONObject();
283+
responseJSON.put(ERROR_MESSAGE, errResp);
284+
}
265285
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
266286
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
267287
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));

0 commit comments

Comments
 (0)