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

Commit 73bbbcc

Browse files
authored
Merge pull request #493 from cloudant/474-476-exceptions
474 476 exceptions
2 parents 8280389 + 344533c commit 73bbbcc

7 files changed

Lines changed: 14 additions & 11 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22
- [FIXED] Create an array of strings for QueryBuilder.fields() when a single field is provided.
3+
- [FIXED] Potential NPE creating exception messages.
34
- [IMPROVED] Return exceptions directly from IAM token request failures instead of logging and
45
deferring the request to the service with no credentials. The exception type is the same, but
56
the message and cause are more clear and a round trip is avoided.

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/main/java/com/cloudant/client/org/lightcouch/CouchDbException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public String getReason() {
7777
public String getMessage() {
7878
String msg = super.getMessage();
7979
// trim trailing full stop
80-
msg = (msg.endsWith(".")) ? msg.substring(0, msg.length() - 1) : msg;
80+
msg = (msg != null && msg.endsWith(".")) ? msg.substring(0, msg.length() - 1) : msg;
8181

8282
// include the status code, URL, error and reason (if available)
8383
return ((getStatusCode() > 0) ? getStatusCode() + " " : "") + msg

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)