Skip to content
Closed
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
9 changes: 8 additions & 1 deletion src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,14 @@ void setError(String 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));

int errCode = 0;
try {
errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE));
} catch (NumberFormatException e) {
// in case of missing error code, we keep it 0
}

connectionRequest.onRequestFailed(responseJSON, errCode, callBackObject);
}

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

import static org.junit.jupiter.api.Assertions.assertEquals;

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

class CSHttpConnectionTest {

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 setError() {
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();

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

assertEquals("Something bad", csConnectionRequest.error.getString("error_message"));
assertEquals(0, csConnectionRequest.statusCode);
}
}