Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 344533c

Browse files
committed
Ensure that HTTP status code is set on CouchDBException
1 parent 805b263 commit 344533c

5 files changed

Lines changed: 12 additions & 10 deletions

File tree

cloudant-client/src/main/java/com/cloudant/client/org/lightcouch/CouchDbClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public HttpConnection execute(HttpConnection connection) {
564564
exception.error = e.error;
565565
}
566566
} else {
567-
exception = new CouchDbException(e.getMessage(), e);
567+
exception = new CouchDbException(e.getMessage(), e, e.statusCode);
568568
exception.error = e.error;
569569
exception.reason = e.reason;
570570
}

cloudant-client/src/test/java/com/cloudant/tests/HttpIamTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public void iamRenewalFailureOnIamToken() throws Exception {
319319
assertThrows(CouchDbException.class,
320320
() -> c.executeRequest(Http.GET(c.getBaseUri())).responseAsString(),
321321
"Failure to get a token should throw a CouchDbException.");
322-
assertTrue(re.getMessage().startsWith("HTTP response error getting session"), "The " +
322+
assertTrue(re.getMessage().startsWith("500 HTTP response error getting session"), "The " +
323323
"exception should have been for a HTTP response error.");
324324

325325
// cloudant mock server
@@ -570,10 +570,8 @@ public void iamTokenServer429RetryAndFail() throws Exception {
570570
assertThrows(CouchDbException.class,
571571
() -> c.executeRequest(Http.GET(c.getBaseUri())).responseAsString(),
572572
"Failure to get a token should throw a CouchDbException.");
573-
assertTrue(re.getMessage().startsWith("HTTP response error getting session"), "The " +
574-
"exception should have been for a HTTP response error.");
575-
assertTrue(re.getMessage().contains("response code 429"), "The exception should report a " +
576-
"429 response code");
573+
assertTrue(re.getMessage().startsWith("429 HTTP response error getting session"), "The " +
574+
"exception should have been for a 429 HTTP response error.");
577575

578576
// iam mock server
579577

cloudant-client/src/test/java/com/cloudant/tests/HttpTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ public void badCredsCookieThrows() {
10501050
() -> c.executeRequest(Http.GET(c.getBaseUri())).responseAsString(),
10511051
"Bad credentials should throw a CouchDbException.");
10521052

1053-
assertTrue(re.getMessage().startsWith("Credentials are incorrect for server"), "The " +
1053+
assertTrue(re.getMessage().startsWith("401 Credentials are incorrect for server"), "The " +
10541054
"exception should have been for bad creds.");
10551055
}
10561056

cloudant-http/src/main/java/com/cloudant/http/internal/interceptors/CookieInterceptorBase.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,17 @@ HttpConnection makeSessionRequest(URL url, byte[] payload, String contentMimeTyp
141141
.getErrorStream());
142142
// Log the error stream content
143143
logger.fine(error);
144+
HttpConnectionInterceptorException e;
144145
if (responseCode == 401) {
145-
throw new HttpConnectionInterceptorException(String.format("Credentials are " +
146+
e = new HttpConnectionInterceptorException(String.format("Credentials are " +
146147
"incorrect for server %s", url));
147148
} else {
148149
// catch any other response code
149-
throw new HttpConnectionInterceptorException(String.format("HTTP response " +
150-
"error getting session at %s, response code %s", url, responseCode));
150+
e = new HttpConnectionInterceptorException(String.format("HTTP response " +
151+
"error getting session at %s.", url));
151152
}
153+
e.statusCode = responseCode;
154+
throw e;
152155
}
153156
} catch (IOException e) {
154157
throw wrapIOException("Failed to read server response from ", conn.getConnection(), e);

cloudant-http/src/main/java/com/cloudant/http/internal/interceptors/HttpConnectionInterceptorException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class HttpConnectionInterceptorException extends RuntimeException {
1919
public boolean deserialize = false;
2020
public final String error;
2121
public final String reason;
22+
public int statusCode;
2223

2324
public HttpConnectionInterceptorException(Throwable cause) {
2425
this.initCause(cause);

0 commit comments

Comments
 (0)