Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #170

Merged
merged 3 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Added skip limit methods for Assets
- Resolved a bug
- Github issue fixed

## v2.0.2

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,26 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
}

void setError(String errResp) {

if (errResp == null || errResp.trim().isEmpty()) {
errResp = "Unexpected error: No response received from server.";
}
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));
int errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE));
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE, "An unknown error occurred."));
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE, "0"));
responseJSON.put(ERRORS, responseJSON.optString(ERRORS, "No additional error details available."));
int errCode = 0;
try {
errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE, "0"));
} catch (NumberFormatException e) {
// Default error code remains 0 if parsing fails
}
connectionRequest.onRequestFailed(responseJSON, errCode, callBackObject);
}

Expand Down
107 changes: 107 additions & 0 deletions src/test/java/com/contentstack/sdk/TestCSHttpConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.contentstack.sdk;

import org.junit.jupiter.api.*;
import org.json.JSONObject;

class TestCSHttpConnection {

static class MockIRequestModelHTTP implements IRequestModelHTTP {
public JSONObject error;
public int statusCode;

@Override
public void sendRequest() {
// Do nothing
}

@Override
public void onRequestFailed(JSONObject error, int statusCode, ResultCallBack callBackObject) {
this.error = error;
this.statusCode = statusCode;
}

@Override
public void onRequestFinished(CSHttpConnection request) {
// Do nothing
}
}

@Test
void testValidJsonErrorResponse() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError("{\"error_message\": \"Invalid API Key\", \"error_code\": \"401\"}");

Assertions.assertEquals("Invalid API Key", csConnectionRequest.error.getString("error_message"));
Assertions.assertEquals(401, csConnectionRequest.error.getInt("error_code"));
}

@Test
void testInvalidJsonErrorResponse() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError("This is not a JSON");

Assertions.assertEquals("This is not a JSON", csConnectionRequest.error.getString("error_message"));
Assertions.assertEquals(0, csConnectionRequest.statusCode);
}

@Test
void testMissingErrorMessageField() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError("{\"error_code\": \"500\"}");

Assertions.assertTrue(csConnectionRequest.error.has("error_message"));
Assertions.assertEquals("An unknown error occurred.", csConnectionRequest.error.optString("error_message", "An unknown error occurred"));
Assertions.assertEquals(500, csConnectionRequest.error.getInt("error_code"));
}

@Test
void testMissingErrorCodeField() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError("{\"error_message\": \"Server error\"}");

Assertions.assertEquals("Server error", csConnectionRequest.error.getString("error_message"));
Assertions.assertEquals(0, csConnectionRequest.statusCode); // Default value when error_code is missing
}

@Test
void testCompletelyEmptyErrorResponse() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError("{}");

Assertions.assertEquals("An unknown error occurred.", csConnectionRequest.error.optString("error_message", "An unknown error occurred"));
Assertions.assertEquals(0, csConnectionRequest.statusCode);
}

@Test
void testNullErrorResponse() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError(null);

Assertions.assertEquals("Unexpected error: No response received from server.", csConnectionRequest.error.getString("error_message"));
Assertions.assertEquals(0, csConnectionRequest.statusCode);
}

@Test
void testErrorResponseWithAdditionalFields() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

CSHttpConnection connection = new CSHttpConnection("https://www.example.com", csConnectionRequest);
connection.setError("{\"error_message\": \"Bad Request\", \"error_code\": \"400\", \"errors\": \"Missing parameter\"}");

Assertions.assertEquals("Bad Request", csConnectionRequest.error.getString("error_message"));
Assertions.assertEquals(400, csConnectionRequest.error.getInt("error_code"));
Assertions.assertEquals("Missing parameter", csConnectionRequest.error.getString("errors"));
}
}
Loading