|
1 | 1 | package com.contentstack.sdk; |
2 | 2 |
|
3 | | -import okhttp3.Request; |
4 | | -import okhttp3.ResponseBody; |
5 | | - |
6 | | -import org.json.JSONArray; |
7 | | -import org.json.JSONException; |
8 | | -import org.json.JSONObject; |
9 | | - |
10 | | -import retrofit2.Call; |
11 | | -import retrofit2.Response; |
12 | | - |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 5 | +import com.fasterxml.jackson.databind.type.MapType; |
13 | 6 | import java.io.IOException; |
14 | 7 | import java.io.UnsupportedEncodingException; |
15 | | -import java.net.URLEncoder; |
16 | 8 | import java.net.SocketTimeoutException; |
| 9 | +import java.net.URLEncoder; |
17 | 10 | import java.nio.charset.StandardCharsets; |
18 | 11 | import java.util.HashMap; |
19 | 12 | import java.util.Iterator; |
|
22 | 15 | import java.util.logging.Level; |
23 | 16 | import java.util.logging.Logger; |
24 | 17 | import java.util.stream.IntStream; |
25 | | -import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing |
26 | | -import com.fasterxml.jackson.databind.json.JsonMapper; |
27 | | -import com.fasterxml.jackson.databind.node.ObjectNode; |
28 | | -import com.fasterxml.jackson.databind.type.MapType; |
| 18 | +import okhttp3.Request; |
| 19 | +import okhttp3.ResponseBody; |
| 20 | +import org.json.JSONArray; |
| 21 | +import org.json.JSONException; |
| 22 | +import org.json.JSONObject; |
| 23 | +import retrofit2.Call; |
| 24 | +import retrofit2.Response; |
| 25 | + |
| 26 | + |
| 27 | + |
29 | 28 |
|
30 | 29 | import static com.contentstack.sdk.Constants.*; |
31 | 30 |
|
@@ -230,7 +229,7 @@ private void getService(String requestUrl) throws IOException { |
230 | 229 | MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, |
231 | 230 | Object.class); |
232 | 231 | Map<String, Object> responseMap = mapper.readValue(response.body().string(), type); |
233 | | - |
| 232 | + |
234 | 233 | // Use the custom method to create an ordered JSONObject |
235 | 234 | responseJSON = createOrderedJSONObject(responseMap); |
236 | 235 | if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) { |
@@ -295,17 +294,26 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) { |
295 | 294 | } |
296 | 295 |
|
297 | 296 | void setError(String errResp) { |
| 297 | + |
| 298 | + if (errResp == null || errResp.trim().isEmpty()) { |
| 299 | + errResp = "Unexpected error: No response received from server."; |
| 300 | + } |
298 | 301 | try { |
299 | 302 | responseJSON = new JSONObject(errResp); |
300 | 303 | } catch (JSONException e) { |
301 | 304 | // If errResp is not valid JSON, create a new JSONObject with the error message |
302 | 305 | responseJSON = new JSONObject(); |
303 | 306 | responseJSON.put(ERROR_MESSAGE, errResp); |
304 | 307 | } |
305 | | - responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE)); |
306 | | - responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE)); |
307 | | - responseJSON.put(ERRORS, responseJSON.optString(ERRORS)); |
308 | | - int errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE)); |
| 308 | + responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE, "An unknown error occurred.")); |
| 309 | + responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE, "0")); |
| 310 | + responseJSON.put(ERRORS, responseJSON.optString(ERRORS, "No additional error details available.")); |
| 311 | + int errCode = 0; |
| 312 | + try { |
| 313 | + errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE, "0")); |
| 314 | + } catch (NumberFormatException e) { |
| 315 | + // Default error code remains 0 if parsing fails |
| 316 | + } |
309 | 317 | connectionRequest.onRequestFailed(responseJSON, errCode, callBackObject); |
310 | 318 | } |
311 | 319 |
|
|
0 commit comments