Skip to content

Commit 5ccc398

Browse files
authored
Merge pull request #170 from contentstack/development
Development
2 parents 43d46e6 + 9600b58 commit 5ccc398

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Added skip limit methods for Assets
88
- Resolved a bug
9+
- Github issue fixed
910

1011
## v2.0.2
1112

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,26 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
294294
}
295295

296296
void setError(String errResp) {
297+
298+
if (errResp == null || errResp.trim().isEmpty()) {
299+
errResp = "Unexpected error: No response received from server.";
300+
}
297301
try {
298302
responseJSON = new JSONObject(errResp);
299303
} catch (JSONException e) {
300304
// If errResp is not valid JSON, create a new JSONObject with the error message
301305
responseJSON = new JSONObject();
302306
responseJSON.put(ERROR_MESSAGE, errResp);
303307
}
304-
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
305-
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
306-
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));
307-
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+
}
308317
connectionRequest.onRequestFailed(responseJSON, errCode, callBackObject);
309318
}
310319

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.contentstack.sdk;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.json.JSONObject;
5+
6+
class TestCSHttpConnection {
7+
8+
static class MockIRequestModelHTTP implements IRequestModelHTTP {
9+
public JSONObject error;
10+
public int statusCode;
11+
12+
@Override
13+
public void sendRequest() {
14+
// Do nothing
15+
}
16+
17+
@Override
18+
public void onRequestFailed(JSONObject error, int statusCode, ResultCallBack callBackObject) {
19+
this.error = error;
20+
this.statusCode = statusCode;
21+
}
22+
23+
@Override
24+
public void onRequestFinished(CSHttpConnection request) {
25+
// Do nothing
26+
}
27+
}
28+
29+
@Test
30+
void testValidJsonErrorResponse() {
31+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
32+
33+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
34+
connection.setError("{\"error_message\": \"Invalid API Key\", \"error_code\": \"401\"}");
35+
36+
Assertions.assertEquals("Invalid API Key", csConnectionRequest.error.getString("error_message"));
37+
Assertions.assertEquals(401, csConnectionRequest.error.getInt("error_code"));
38+
}
39+
40+
@Test
41+
void testInvalidJsonErrorResponse() {
42+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
43+
44+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
45+
connection.setError("This is not a JSON");
46+
47+
Assertions.assertEquals("This is not a JSON", csConnectionRequest.error.getString("error_message"));
48+
Assertions.assertEquals(0, csConnectionRequest.statusCode);
49+
}
50+
51+
@Test
52+
void testMissingErrorMessageField() {
53+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
54+
55+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
56+
connection.setError("{\"error_code\": \"500\"}");
57+
58+
Assertions.assertTrue(csConnectionRequest.error.has("error_message"));
59+
Assertions.assertEquals("An unknown error occurred.", csConnectionRequest.error.optString("error_message", "An unknown error occurred"));
60+
Assertions.assertEquals(500, csConnectionRequest.error.getInt("error_code"));
61+
}
62+
63+
@Test
64+
void testMissingErrorCodeField() {
65+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
66+
67+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
68+
connection.setError("{\"error_message\": \"Server error\"}");
69+
70+
Assertions.assertEquals("Server error", csConnectionRequest.error.getString("error_message"));
71+
Assertions.assertEquals(0, csConnectionRequest.statusCode); // Default value when error_code is missing
72+
}
73+
74+
@Test
75+
void testCompletelyEmptyErrorResponse() {
76+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
77+
78+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
79+
connection.setError("{}");
80+
81+
Assertions.assertEquals("An unknown error occurred.", csConnectionRequest.error.optString("error_message", "An unknown error occurred"));
82+
Assertions.assertEquals(0, csConnectionRequest.statusCode);
83+
}
84+
85+
@Test
86+
void testNullErrorResponse() {
87+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
88+
89+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
90+
connection.setError(null);
91+
92+
Assertions.assertEquals("Unexpected error: No response received from server.", csConnectionRequest.error.getString("error_message"));
93+
Assertions.assertEquals(0, csConnectionRequest.statusCode);
94+
}
95+
96+
@Test
97+
void testErrorResponseWithAdditionalFields() {
98+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
99+
100+
CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
101+
connection.setError("{\"error_message\": \"Bad Request\", \"error_code\": \"400\", \"errors\": \"Missing parameter\"}");
102+
103+
Assertions.assertEquals("Bad Request", csConnectionRequest.error.getString("error_message"));
104+
Assertions.assertEquals(400, csConnectionRequest.error.getInt("error_code"));
105+
Assertions.assertEquals("Missing parameter", csConnectionRequest.error.getString("errors"));
106+
}
107+
}

0 commit comments

Comments
 (0)