Skip to content

Commit 69a87fc

Browse files
fix ambiguous error codes
OKTA-435843 <<<Jenkins Check-In of Tested SHA: ab12651 for eng_productivity_ci_bot_okta@okta.com>>> Artifact: okta-oidc-android Files changed count: 3 PR Link: "#293"
1 parent 6e0596b commit 69a87fc

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

library/src/main/java/com/okta/oidc/net/HttpResponse.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,28 @@ public JSONObject asJson() throws IOException, JSONException {
111111
mStatusCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
112112
throw new HttpStatusCodeException(mStatusCode, mHttpClient.getResponseMessage());
113113
}
114-
InputStream is = getContent();
115-
if (is == null) {
114+
return getJsonObjectFromResponseInputStream(getContent());
115+
}
116+
117+
public JSONObject asJsonWithErrorDescription() throws IOException, JSONException {
118+
if (mStatusCode < HttpURLConnection.HTTP_OK ||
119+
mStatusCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
120+
try {
121+
return getJsonObjectFromResponseInputStream(getContent());
122+
} catch (Exception any) {
123+
throw new HttpStatusCodeException(mStatusCode, mHttpClient.getResponseMessage());
124+
}
125+
}
126+
return getJsonObjectFromResponseInputStream(getContent());
127+
}
128+
129+
private static JSONObject getJsonObjectFromResponseInputStream(
130+
final InputStream inputStream
131+
) throws IOException, JSONException {
132+
if (inputStream == null) {
116133
throw new IOException("Input stream must not be null");
117134
}
118-
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
135+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
119136
Writer writer = new StringWriter();
120137
String line = reader.readLine();
121138
while (line != null) {

library/src/main/java/com/okta/oidc/net/request/TokenRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public TokenResponse executeRequest(OktaHttpClient client) throws AuthorizationE
128128
TokenResponse tokenResponse;
129129
try {
130130
response = openConnection(client);
131-
JSONObject json = response.asJson();
131+
JSONObject json = response.asJsonWithErrorDescription();
132132
if (json.has(AuthorizationException.PARAM_ERROR)) {
133133
try {
134134
final String error = json.getString(AuthorizationException.PARAM_ERROR);

library/src/test/java/com/okta/oidc/clients/sessions/SessionClientImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ public void refreshTokenFailure() throws InterruptedException {
232232
latch.await();
233233
assertNull(cb.getResult());
234234
assertNotNull(cb.getException());
235-
assertEquals(cb.getException().getMessage(), "Invalid status code 401 Client Error");
235+
assertEquals(cb.getException().getMessage(), "No client credentials found.");
236+
assertEquals(cb.getException().type, TYPE_OAUTH_TOKEN_ERROR);
236237
}
237238

238239
@Test

0 commit comments

Comments
 (0)