Skip to content

Commit

Permalink
fix ambiguous error codes
Browse files Browse the repository at this point in the history
OKTA-435843
<<<Jenkins Check-In of Tested SHA: ab12651 for [email protected]>>>
Artifact: okta-oidc-android
Files changed count: 3
PR Link: "#293"
  • Loading branch information
NikitaAvraimov-okta authored and eng-prod-CI-bot-okta committed Dec 22, 2021
1 parent 6e0596b commit 69a87fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
23 changes: 20 additions & 3 deletions library/src/main/java/com/okta/oidc/net/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,28 @@ public JSONObject asJson() throws IOException, JSONException {
mStatusCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
throw new HttpStatusCodeException(mStatusCode, mHttpClient.getResponseMessage());
}
InputStream is = getContent();
if (is == null) {
return getJsonObjectFromResponseInputStream(getContent());
}

public JSONObject asJsonWithErrorDescription() throws IOException, JSONException {
if (mStatusCode < HttpURLConnection.HTTP_OK ||
mStatusCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
try {
return getJsonObjectFromResponseInputStream(getContent());
} catch (Exception any) {
throw new HttpStatusCodeException(mStatusCode, mHttpClient.getResponseMessage());
}
}
return getJsonObjectFromResponseInputStream(getContent());
}

private static JSONObject getJsonObjectFromResponseInputStream(
final InputStream inputStream
) throws IOException, JSONException {
if (inputStream == null) {
throw new IOException("Input stream must not be null");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Writer writer = new StringWriter();
String line = reader.readLine();
while (line != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public TokenResponse executeRequest(OktaHttpClient client) throws AuthorizationE
TokenResponse tokenResponse;
try {
response = openConnection(client);
JSONObject json = response.asJson();
JSONObject json = response.asJsonWithErrorDescription();
if (json.has(AuthorizationException.PARAM_ERROR)) {
try {
final String error = json.getString(AuthorizationException.PARAM_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public void refreshTokenFailure() throws InterruptedException {
latch.await();
assertNull(cb.getResult());
assertNotNull(cb.getException());
assertEquals(cb.getException().getMessage(), "Invalid status code 401 Client Error");
assertEquals(cb.getException().getMessage(), "No client credentials found.");
assertEquals(cb.getException().type, TYPE_OAUTH_TOKEN_ERROR);
}

@Test
Expand Down

0 comments on commit 69a87fc

Please sign in to comment.