Skip to content

Commit

Permalink
handle missing error code (0 as default)
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Jan 23, 2025
1 parent 5e09340 commit a45576a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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);
}
}

0 comments on commit a45576a

Please sign in to comment.