From 158a88551d5659da5a5e55930839b3e944c1ebb2 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 2 Apr 2018 14:46:00 -0700 Subject: [PATCH 01/18] init for better error objects --- .../java/com/box/sdk/BoxAPIException.java | 33 ++++++++++++++++++- .../com/box/sdk/BoxAPIResponseException.java | 7 ++++ .../com/box/sdk/BoxValidationException.java | 7 ++++ src/test/java/com/box/sdk/BoxFolderTest.java | 14 +++++++- 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/box/sdk/BoxAPIResponseException.java create mode 100644 src/main/java/com/box/sdk/BoxValidationException.java diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index 2c74734a8..675671e33 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -13,6 +13,8 @@ public class BoxAPIException extends RuntimeException { private final int responseCode; private final String response; private final Map> headers; + private final String message; + private BoxAPIResponse responseObj; /** * Constructs a BoxAPIException with a specified message. @@ -22,8 +24,10 @@ public BoxAPIException(String message) { super(message); this.responseCode = 0; - this.response = null; this.headers = null; + this.response = null; + this.message = message; + this.responseObj = null; } /** @@ -36,6 +40,7 @@ public BoxAPIException(String message, int responseCode, String response) { //People are missing the getResponse method we have. So adding it to message super(message + "\n" + response); + this.message = message; this.responseCode = responseCode; this.response = response; this.headers = null; @@ -56,6 +61,8 @@ public BoxAPIException(String message, int responseCode, String responseBody, this.responseCode = responseCode; this.response = responseBody; this.headers = responseHeaders; + this.message = message; + this.responseObj = null; } /** @@ -69,6 +76,27 @@ public BoxAPIException(String message, Throwable cause) { this.responseCode = 0; this.response = null; this.headers = null; + this.message = message; + this.responseObj = null; + } + + /** + * Constructs a BoxAPIException that contains detailed message for underlying exception. + * @param response the response body returned by the Box server. + */ + public BoxAPIException(String message, BoxAPIResponse response) { + if(response instanceof BoxJSONResponse) { + ((BoxJSONResponse) response).getJSON(); + } + + String requestId; + String boxRequestId; + + this.message = message + "The API returned an unexpected response:[" + response.getResponseCode() + "|" + "]"; + this.responseObj = response; + this.response = response.bodyToString(); + this.responseCode = response.getResponseCode(); + this.headers = null; } /** @@ -81,6 +109,7 @@ public BoxAPIException(String message, Throwable cause) { public BoxAPIException(String message, int responseCode, String response, Throwable cause) { super(message, cause); + this.message = message; this.responseCode = responseCode; this.response = response; this.headers = null; @@ -102,6 +131,8 @@ public BoxAPIException(String message, int responseCode, String responseBody, this.responseCode = responseCode; this.response = responseBody; this.headers = responseHeaders; + this.message = message; + this.responseObj = null; } /** diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java new file mode 100644 index 000000000..e16bb527d --- /dev/null +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -0,0 +1,7 @@ +package com.box.sdk; + +/** + * + */ +public class BoxAPIResponseException { +} diff --git a/src/main/java/com/box/sdk/BoxValidationException.java b/src/main/java/com/box/sdk/BoxValidationException.java new file mode 100644 index 000000000..48dcb721d --- /dev/null +++ b/src/main/java/com/box/sdk/BoxValidationException.java @@ -0,0 +1,7 @@ +package com.box.sdk; + +/** + * + */ +public class BoxValidationException { +} diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index 7edaa05d0..4dc16c749 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -18,6 +18,18 @@ import java.text.SimpleDateFormat; import java.util.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.isEmptyOrNullString; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.*; @@ -634,7 +646,7 @@ public void testCreateWeblinkParseAllFieldsCorrectly() throws ParseException, Ma @Test @Category(IntegrationTest.class) public void creatingAndDeletingFolderSucceeds() { - BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken()); + BoxAPIConnection api = new BoxAPIConnection("h9oIx1vHwSGFTCoD6uZXwCuDBrCtuAKk"); BoxFolder rootFolder = BoxFolder.getRootFolder(api); BoxFolder childFolder = rootFolder.createFolder("[creatingAndDeletingFolderSucceeds] Ĥȅľľő Ƒŕőďő") .getResource(); From 4f48fdba4d612b1f6fcad0a4767cc7bea9dcd357 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Tue, 3 Apr 2018 10:23:12 -0700 Subject: [PATCH 02/18] error classes --- .../java/com/box/sdk/BoxAPIResponseException.java | 4 +++- .../java/com/box/sdk/BoxValidationException.java | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index e16bb527d..fdbb7119e 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -1,7 +1,9 @@ package com.box.sdk; /** - * + * Thrown to indicate than an error occured while returning with a response from the Box API. */ public class BoxAPIResponseException { + + } diff --git a/src/main/java/com/box/sdk/BoxValidationException.java b/src/main/java/com/box/sdk/BoxValidationException.java index 48dcb721d..66abb9725 100644 --- a/src/main/java/com/box/sdk/BoxValidationException.java +++ b/src/main/java/com/box/sdk/BoxValidationException.java @@ -1,7 +1,19 @@ package com.box.sdk; /** - * + * Thrown to indicate that an error occurred while validating a Box API call. */ public class BoxValidationException { + private final String message; + + /** + * Constructs a BoxValidationException with a specified message. + * @param message a message explaining why the exception occurred. + */ + public BoxValidationException(String message) { + + this.message = message; + } + + } From bd01b60aceb4ff59b6375bde11751e2dcc41772e Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Thu, 5 Apr 2018 13:14:46 -0700 Subject: [PATCH 03/18] first implementation of exceptions --- .../com/box/sdk/BoxAPIResponseException.java | 44 ++++++++++++++++++- .../com/box/sdk/BoxValidationException.java | 4 +- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index fdbb7119e..0ad0730c5 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -3,7 +3,49 @@ /** * Thrown to indicate than an error occured while returning with a response from the Box API. */ -public class BoxAPIResponseException { +public class BoxAPIResponseException extends RuntimeException{ + private final String message; + private final String response; + private BoxAPIResponse responseObj; + + /** + * Constructs a BoxAPIResponseException with a specified message. + * @param message a message explaining why the exception occurred. + */ + public BoxAPIResponseException(String message) { + super(message); + + this.message = message; + this.response = null; + this.responseObj = null; + } + + /** + * Constructs a BoxAPIResponseException with a detailed message for underlying exception. + * @param message a message explaining why the exception occurred. + * @param responseBody the response body returned by the Box server. + */ + public BoxAPIResponseException(String message, String responseBody) { + super(message); + + this.message = message; + this.response = responseBody; + this.responseObj = null; + } + + /** + * Constructs a BoxAPIResponseException that wraps another underlying exception with details about the server's response. + * @param message a message explaining why the exception occurred. + * @param responseBody the response body returned by the Box server. + * @param response the response returned by the Box server. + */ + public BoxAPIResponseException(String message, String responseBody, BoxAPIResponse response) { + super(message); + + this.message = message; + this.response = responseBody; + this.responseObj = response; + } } diff --git a/src/main/java/com/box/sdk/BoxValidationException.java b/src/main/java/com/box/sdk/BoxValidationException.java index 66abb9725..076fd9498 100644 --- a/src/main/java/com/box/sdk/BoxValidationException.java +++ b/src/main/java/com/box/sdk/BoxValidationException.java @@ -4,6 +4,7 @@ * Thrown to indicate that an error occurred while validating a Box API call. */ public class BoxValidationException { + private final String message; /** @@ -11,9 +12,6 @@ public class BoxValidationException { * @param message a message explaining why the exception occurred. */ public BoxValidationException(String message) { - this.message = message; } - - } From 9f1d19433b6a319430142de7aaa6138a332cc492 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Thu, 5 Apr 2018 16:17:42 -0700 Subject: [PATCH 04/18] first implementation of the BoxAPIResponseException --- .../java/com/box/sdk/BoxAPIException.java | 24 ------ .../com/box/sdk/BoxAPIResponseException.java | 76 +++++++++---------- src/test/java/com/box/sdk/BoxFolderTest.java | 2 +- 3 files changed, 36 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index 675671e33..647551b06 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -14,7 +14,6 @@ public class BoxAPIException extends RuntimeException { private final String response; private final Map> headers; private final String message; - private BoxAPIResponse responseObj; /** * Constructs a BoxAPIException with a specified message. @@ -27,7 +26,6 @@ public BoxAPIException(String message) { this.headers = null; this.response = null; this.message = message; - this.responseObj = null; } /** @@ -62,7 +60,6 @@ public BoxAPIException(String message, int responseCode, String responseBody, this.response = responseBody; this.headers = responseHeaders; this.message = message; - this.responseObj = null; } /** @@ -77,26 +74,6 @@ public BoxAPIException(String message, Throwable cause) { this.response = null; this.headers = null; this.message = message; - this.responseObj = null; - } - - /** - * Constructs a BoxAPIException that contains detailed message for underlying exception. - * @param response the response body returned by the Box server. - */ - public BoxAPIException(String message, BoxAPIResponse response) { - if(response instanceof BoxJSONResponse) { - ((BoxJSONResponse) response).getJSON(); - } - - String requestId; - String boxRequestId; - - this.message = message + "The API returned an unexpected response:[" + response.getResponseCode() + "|" + "]"; - this.responseObj = response; - this.response = response.bodyToString(); - this.responseCode = response.getResponseCode(); - this.headers = null; } /** @@ -132,7 +109,6 @@ public BoxAPIException(String message, int responseCode, String responseBody, this.response = responseBody; this.headers = responseHeaders; this.message = message; - this.responseObj = null; } /** diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 0ad0730c5..f67cd240d 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -1,51 +1,45 @@ package com.box.sdk; +import com.eclipsesource.json.JsonObject; + +import java.net.HttpURLConnection; + /** * Thrown to indicate than an error occured while returning with a response from the Box API. */ -public class BoxAPIResponseException extends RuntimeException{ +public class BoxAPIResponseException extends BoxAPIException{ - private final String message; - private final String response; private BoxAPIResponse responseObj; + private String message; /** - * Constructs a BoxAPIResponseException with a specified message. - * @param message a message explaining why the exception occurred. - */ - public BoxAPIResponseException(String message) { - super(message); - - this.message = message; - this.response = null; - this.responseObj = null; - } - - /** - * Constructs a BoxAPIResponseException with a detailed message for underlying exception. - * @param message a message explaining why the exception occurred. - * @param responseBody the response body returned by the Box server. - */ - public BoxAPIResponseException(String message, String responseBody) { - super(message); - - this.message = message; - this.response = responseBody; - this.responseObj = null; - } - - /** - * Constructs a BoxAPIResponseException that wraps another underlying exception with details about the server's response. - * @param message a message explaining why the exception occurred. - * @param responseBody the response body returned by the Box server. - * @param response the response returned by the Box server. - */ - public BoxAPIResponseException(String message, String responseBody, BoxAPIResponse response) { - super(message); - - this.message = message; - this.response = responseBody; - this.responseObj = response; - } - + * Constructs a BoxAPIException that contains detailed message for underlying exception. + * @param response the response body returned by the Box server. + */ + public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { + super(message, responseObj.getResponseCode(), responseObj.bodyToString()); + String requestId = ""; + String apiMessage = ""; + JsonObject responseJSON = null; + + if(responseObj.bodyToString()!=null) { + responseJSON = JsonObject.readFrom(responseObj.bodyToString()); + + if(responseObj.bodyToString()!=null && responseJSON.get("request_id")!=null) { + requestId = " | " + responseJSON.get("request_id").toString(); + } + + if(responseObj.bodyToString()!=null && responseJSON.get("code")!=null) { + apiMessage += " " + responseJSON.get("code").toString(); + } + + if(responseObj.bodyToString()!=null && responseJSON.get("message")!=null) { + apiMessage += " - " + responseJSON.get("message").toString(); + } + + this.message = String.format("%s [%d%s]%s", message, responseObj.getResponseCode(), requestId, apiMessage) + } else { + this.message = message + " [" + responseObj.getResponseCode() + "]"; + } + } } diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index 4dc16c749..3f138200f 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -646,7 +646,7 @@ public void testCreateWeblinkParseAllFieldsCorrectly() throws ParseException, Ma @Test @Category(IntegrationTest.class) public void creatingAndDeletingFolderSucceeds() { - BoxAPIConnection api = new BoxAPIConnection("h9oIx1vHwSGFTCoD6uZXwCuDBrCtuAKk"); + BoxAPIConnection api = new BoxAPIConnection("62nPKxa5ZKJ3UMlP4uDewb4PCHi72j7O"); BoxFolder rootFolder = BoxFolder.getRootFolder(api); BoxFolder childFolder = rootFolder.createFolder("[creatingAndDeletingFolderSucceeds] Ĥȅľľő Ƒŕőďő") .getResource(); From 19a48a064cd3479c25d15b398354eabeaf99f2b5 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Fri, 6 Apr 2018 18:13:08 -0700 Subject: [PATCH 05/18] test for BoxAPIResponseException --- .../java/com/box/sdk/BoxAPIException.java | 10 +-- src/main/java/com/box/sdk/BoxAPIResponse.java | 22 +++++- .../com/box/sdk/BoxAPIResponseException.java | 23 ++++-- src/main/java/com/box/sdk/BoxFolder.java | 2 - .../com/box/sdk/BoxValidationException.java | 17 ----- .../box/sdk/BoxAPIResponseExceptionTest.java | 72 +++++++++++++++++++ 6 files changed, 114 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/com/box/sdk/BoxValidationException.java create mode 100644 src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index 647551b06..0f348e7af 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -8,12 +8,12 @@ * Thrown to indicate that an error occurred while communicating with the Box API. */ public class BoxAPIException extends RuntimeException { - private static final long serialVersionUID = 1L; - private final int responseCode; - private final String response; - private final Map> headers; - private final String message; + private static final long serialVersionUID = 1L; + protected int responseCode; + protected String response; + protected Map> headers; + protected String message; /** * Constructs a BoxAPIException with a specified message. diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index 2eece3715..73e025d29 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -5,6 +5,7 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -70,19 +71,26 @@ public BoxAPIResponse(int responseCode, Map headers) { */ public BoxAPIResponse(HttpURLConnection connection) { this.connection = connection; - this.headers = null; this.inputStream = null; + try { this.responseCode = this.connection.getResponseCode(); } catch (IOException e) { throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e); } + Map responseHeaders = new HashMap(); + for(String headerKey : connection.getHeaderFields().keySet()) { + responseHeaders.put(headerKey, connection.getHeaderField(headerKey)); + } + this.headers = responseHeaders; + if (!isSuccess(this.responseCode)) { this.logResponse(); - throw new BoxAPIException("The API returned an error code: " + this.responseCode, this.responseCode, - this.bodyToString(), this.connection.getHeaderFields()); +// throw new BoxAPIException("The API returned an error code: " + this.responseCode, this.responseCode, +// this.bodyToString(), this.connection.getHeaderFields()); + throw new BoxAPIResponseException("The API returned an error code: ", this); } this.logResponse(); @@ -194,6 +202,14 @@ public void disconnect() { } } + /** + * + * @return + */ + public Map getHeaders() { + return this.headers; + } + @Override public String toString() { String lineSeparator = System.getProperty("line.separator"); diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index f67cd240d..e07bbf914 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -3,6 +3,10 @@ import com.eclipsesource.json.JsonObject; import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Thrown to indicate than an error occured while returning with a response from the Box API. @@ -10,7 +14,6 @@ public class BoxAPIResponseException extends BoxAPIException{ private BoxAPIResponse responseObj; - private String message; /** * Constructs a BoxAPIException that contains detailed message for underlying exception. @@ -21,23 +24,33 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { String requestId = ""; String apiMessage = ""; JsonObject responseJSON = null; + this.responseObj = responseObj; + + Map> responseHeaders = new HashMap>(); + for(String headerKey : responseObj.getHeaders().keySet()) { + List headerValues = new ArrayList(); + headerValues.add(responseObj.getHeaderField(headerKey)); + responseHeaders.put(headerKey, headerValues); + } + this.headers = responseHeaders; if(responseObj.bodyToString()!=null) { responseJSON = JsonObject.readFrom(responseObj.bodyToString()); if(responseObj.bodyToString()!=null && responseJSON.get("request_id")!=null) { - requestId = " | " + responseJSON.get("request_id").toString(); + requestId = " | " + responseJSON.get("request_id").asString(); } if(responseObj.bodyToString()!=null && responseJSON.get("code")!=null) { - apiMessage += " " + responseJSON.get("code").toString(); + apiMessage += " " + responseJSON.get("code").asString(); } if(responseObj.bodyToString()!=null && responseJSON.get("message")!=null) { - apiMessage += " - " + responseJSON.get("message").toString(); + apiMessage += " - " + responseJSON.get("message").asString(); } - this.message = String.format("%s [%d%s]%s", message, responseObj.getResponseCode(), requestId, apiMessage) + this.message = message + " [" + responseObj.getResponseCode() + requestId + "] " + apiMessage; + } else { this.message = message + " [" + responseObj.getResponseCode() + "]"; } diff --git a/src/main/java/com/box/sdk/BoxFolder.java b/src/main/java/com/box/sdk/BoxFolder.java index 38f3f4f09..74de9d4b4 100644 --- a/src/main/java/com/box/sdk/BoxFolder.java +++ b/src/main/java/com/box/sdk/BoxFolder.java @@ -239,8 +239,6 @@ public Collection getCollaborations() { return collaborations; } - - @Override public BoxFolder.Info getInfo() { URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); diff --git a/src/main/java/com/box/sdk/BoxValidationException.java b/src/main/java/com/box/sdk/BoxValidationException.java deleted file mode 100644 index 076fd9498..000000000 --- a/src/main/java/com/box/sdk/BoxValidationException.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.box.sdk; - -/** - * Thrown to indicate that an error occurred while validating a Box API call. - */ -public class BoxValidationException { - - private final String message; - - /** - * Constructs a BoxValidationException with a specified message. - * @param message a message explaining why the exception occurred. - */ - public BoxValidationException(String message) { - this.message = message; - } -} diff --git a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java new file mode 100644 index 000000000..675fc0904 --- /dev/null +++ b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java @@ -0,0 +1,72 @@ +package com.box.sdk; + +import java.net.MalformedURLException; +import java.net.URL; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import com.eclipsesource.json.JsonObject; +import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +/** + * + */ +public class BoxAPIResponseExceptionTest { + @Rule + public WireMockRule wireMockRule = new WireMockRule(53620); + + @Test + @Category(UnitTest.class) + public void testAPIResponseExceptionReturnsCorrectErrorMessage() throws MalformedURLException{ + BoxAPIConnection api = new BoxAPIConnection(""); + + final JsonObject fakeJSONResponse = JsonObject.readFrom("{\n" + + " \"type\": \"error\",\n" + + " \"status\": \"409\",\n" + + " \"code\": \"item_name_in_use\",\n" + + " \"context_info\": {\n" + + " \"conflicts\": [\n" + + " {\n" + + " \"type\": \"folder\",\n" + + " \"id\": \"12345\",\n" + + " \"sequence_id\": \"1\",\n" + + " \"etag\": \"1\",\n" + + " \"name\": \"Helpful things\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"help_url\": \"http://developers.box.com/docs/#errors\",\n" + + " \"message\": \"Item with the same name already exists\",\n" + + " \"request_id\": \"5678\"\n" + + " }"); + + stubFor(post(urlEqualTo("/folders")) + .willReturn(aResponse() + .withStatus(409) + .withHeader("Content-Type", "application/json") + .withBody(fakeJSONResponse.toString()))); + + URL url = new URL("http://localhost:53620/folders"); + BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); + + try { + BoxJSONResponse response = (BoxJSONResponse) request.send(); + } catch (BoxAPIResponseException e) { + Assert.assertEquals(409, e.responseCode); + Assert.assertEquals("The API returned an error code: [409 | 5678] item_name_in_use - Item with the same name already exists", e.message); + return; + } + + Assert.fail("Never threw a BoxAPIResponseException"); + } +} From a5a81306ba794e9a3d76109b5c2fe8080714a569 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Fri, 6 Apr 2018 18:43:14 -0700 Subject: [PATCH 06/18] additional check exception response body and added test case for empty body --- src/main/java/com/box/sdk/BoxAPIResponse.java | 2 +- .../com/box/sdk/BoxAPIResponseException.java | 4 +- .../box/sdk/BoxAPIResponseExceptionTest.java | 68 ++++++++++++++++++- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index 73e025d29..0c794bdaa 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -90,7 +90,7 @@ public BoxAPIResponse(HttpURLConnection connection) { this.logResponse(); // throw new BoxAPIException("The API returned an error code: " + this.responseCode, this.responseCode, // this.bodyToString(), this.connection.getHeaderFields()); - throw new BoxAPIResponseException("The API returned an error code: ", this); + throw new BoxAPIResponseException("The API returned an error code", this); } this.logResponse(); diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index e07bbf914..e5d095a34 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -34,7 +34,7 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { } this.headers = responseHeaders; - if(responseObj.bodyToString()!=null) { + if(responseObj.bodyToString()!=null && !responseObj.bodyToString().equals("")) { responseJSON = JsonObject.readFrom(responseObj.bodyToString()); if(responseObj.bodyToString()!=null && responseJSON.get("request_id")!=null) { @@ -49,7 +49,7 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { apiMessage += " - " + responseJSON.get("message").asString(); } - this.message = message + " [" + responseObj.getResponseCode() + requestId + "] " + apiMessage; + this.message = message + " [" + responseObj.getResponseCode() + requestId + "]" + apiMessage; } else { this.message = message + " [" + responseObj.getResponseCode() + "]"; diff --git a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java index 675fc0904..ca2d57ec1 100644 --- a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java @@ -63,7 +63,73 @@ public void testAPIResponseExceptionReturnsCorrectErrorMessage() throws Malforme BoxJSONResponse response = (BoxJSONResponse) request.send(); } catch (BoxAPIResponseException e) { Assert.assertEquals(409, e.responseCode); - Assert.assertEquals("The API returned an error code: [409 | 5678] item_name_in_use - Item with the same name already exists", e.message); + Assert.assertEquals("The API returned an error code [409 | 5678] item_name_in_use - Item with the same name already exists", e.message); + return; + } + + Assert.fail("Never threw a BoxAPIResponseException"); + } + + @Test + @Category(UnitTest.class) + public void testAPIResponseExceptionMissingFieldsReturnsCorrectErrorMessage() throws MalformedURLException{ + BoxAPIConnection api = new BoxAPIConnection(""); + + final JsonObject fakeJSONResponse = JsonObject.readFrom("{\n" + + " \"type\": \"error\",\n" + + " \"status\": \"409\",\n" + + " \"context_info\": {\n" + + " \"conflicts\": [\n" + + " {\n" + + " \"type\": \"folder\",\n" + + " \"id\": \"12345\",\n" + + " \"sequence_id\": \"1\",\n" + + " \"etag\": \"1\",\n" + + " \"name\": \"Helpful things\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"help_url\": \"http://developers.box.com/docs/#errors\"\n" + + " }"); + + stubFor(post(urlEqualTo("/folders")) + .willReturn(aResponse() + .withStatus(409) + .withHeader("Content-Type", "application/json") + .withBody(fakeJSONResponse.toString()))); + + URL url = new URL("http://localhost:53620/folders"); + BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); + + try { + BoxJSONResponse response = (BoxJSONResponse) request.send(); + } catch (BoxAPIResponseException e) { + Assert.assertEquals(409, e.responseCode); + Assert.assertEquals("The API returned an error code [409]", e.message); + return; + } + + Assert.fail("Never threw a BoxAPIResponseException"); + } + + @Test + @Category(UnitTest.class) + public void testAPIResponseExceptionMissingBodyReturnsCorrectErrorMessage() throws MalformedURLException{ + BoxAPIConnection api = new BoxAPIConnection(""); + + stubFor(post(urlEqualTo("/folders")) + .willReturn(aResponse() + .withStatus(403))); + + URL url = new URL("http://localhost:53620/folders"); + BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); + + try { + BoxJSONResponse response = (BoxJSONResponse) request.send(); + } catch (BoxAPIResponseException e) { + Assert.assertEquals(403, e.responseCode); + Assert.assertEquals("", e.response); + Assert.assertEquals("The API returned an error code [403]", e.message); return; } From 01689d6f29bfdae947816fb1c86828c5995a4df0 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 9 Apr 2018 11:07:21 -0700 Subject: [PATCH 07/18] clean up for error object implementation --- src/main/java/com/box/sdk/BoxAPIException.java | 11 ++--------- src/main/java/com/box/sdk/BoxAPIResponse.java | 2 -- src/test/java/com/box/sdk/BoxFolderTest.java | 15 +-------------- 3 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index 0f348e7af..f1ee46624 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -8,12 +8,11 @@ * Thrown to indicate that an error occurred while communicating with the Box API. */ public class BoxAPIException extends RuntimeException { - private static final long serialVersionUID = 1L; + protected int responseCode; protected String response; protected Map> headers; - protected String message; /** * Constructs a BoxAPIException with a specified message. @@ -23,9 +22,8 @@ public BoxAPIException(String message) { super(message); this.responseCode = 0; - this.headers = null; this.response = null; - this.message = message; + this.headers = null; } /** @@ -38,7 +36,6 @@ public BoxAPIException(String message, int responseCode, String response) { //People are missing the getResponse method we have. So adding it to message super(message + "\n" + response); - this.message = message; this.responseCode = responseCode; this.response = response; this.headers = null; @@ -59,7 +56,6 @@ public BoxAPIException(String message, int responseCode, String responseBody, this.responseCode = responseCode; this.response = responseBody; this.headers = responseHeaders; - this.message = message; } /** @@ -73,7 +69,6 @@ public BoxAPIException(String message, Throwable cause) { this.responseCode = 0; this.response = null; this.headers = null; - this.message = message; } /** @@ -86,7 +81,6 @@ public BoxAPIException(String message, Throwable cause) { public BoxAPIException(String message, int responseCode, String response, Throwable cause) { super(message, cause); - this.message = message; this.responseCode = responseCode; this.response = response; this.headers = null; @@ -108,7 +102,6 @@ public BoxAPIException(String message, int responseCode, String responseBody, this.responseCode = responseCode; this.response = responseBody; this.headers = responseHeaders; - this.message = message; } /** diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index 0c794bdaa..454b4dd71 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -88,8 +88,6 @@ public BoxAPIResponse(HttpURLConnection connection) { if (!isSuccess(this.responseCode)) { this.logResponse(); -// throw new BoxAPIException("The API returned an error code: " + this.responseCode, this.responseCode, -// this.bodyToString(), this.connection.getHeaderFields()); throw new BoxAPIResponseException("The API returned an error code", this); } diff --git a/src/test/java/com/box/sdk/BoxFolderTest.java b/src/test/java/com/box/sdk/BoxFolderTest.java index 3f138200f..5eff27580 100644 --- a/src/test/java/com/box/sdk/BoxFolderTest.java +++ b/src/test/java/com/box/sdk/BoxFolderTest.java @@ -18,19 +18,6 @@ import java.text.SimpleDateFormat; import java.util.*; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.isEmptyOrNullString; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -646,7 +633,7 @@ public void testCreateWeblinkParseAllFieldsCorrectly() throws ParseException, Ma @Test @Category(IntegrationTest.class) public void creatingAndDeletingFolderSucceeds() { - BoxAPIConnection api = new BoxAPIConnection("62nPKxa5ZKJ3UMlP4uDewb4PCHi72j7O"); + BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken()); BoxFolder rootFolder = BoxFolder.getRootFolder(api); BoxFolder childFolder = rootFolder.createFolder("[creatingAndDeletingFolderSucceeds] Ĥȅľľő Ƒŕőďő") .getResource(); From c8d228517db8ed1a7ce0633165e9eb4a9f2f64a5 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 9 Apr 2018 11:18:48 -0700 Subject: [PATCH 08/18] fixed function comments and moved message to parent with protected --- src/main/java/com/box/sdk/BoxAPIException.java | 1 + src/main/java/com/box/sdk/BoxAPIResponseException.java | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index f1ee46624..5fd543ba7 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -13,6 +13,7 @@ public class BoxAPIException extends RuntimeException { protected int responseCode; protected String response; protected Map> headers; + protected String message; /** * Constructs a BoxAPIException with a specified message. diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index e5d095a34..a2b9107af 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -17,7 +17,10 @@ public class BoxAPIResponseException extends BoxAPIException{ /** * Constructs a BoxAPIException that contains detailed message for underlying exception. - * @param response the response body returned by the Box server. + * @param message a message explaining why the error occurred. + * @param responseObj a response object from the server. + * @return a {@link BoxAPIResponseException} containing a custom error message that includes a request id, status + * code, and message from the server. */ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { super(message, responseObj.getResponseCode(), responseObj.bodyToString()); From 3fdbb42e2ec67b902f8a71940325a98397ea40c7 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 9 Apr 2018 11:20:35 -0700 Subject: [PATCH 09/18] moved message back down to BoxAPIResponseException --- src/main/java/com/box/sdk/BoxAPIException.java | 1 - src/main/java/com/box/sdk/BoxAPIResponseException.java | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index 5fd543ba7..f1ee46624 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -13,7 +13,6 @@ public class BoxAPIException extends RuntimeException { protected int responseCode; protected String response; protected Map> headers; - protected String message; /** * Constructs a BoxAPIException with a specified message. diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index a2b9107af..55255b888 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -14,6 +14,7 @@ public class BoxAPIResponseException extends BoxAPIException{ private BoxAPIResponse responseObj; + private String message; /** * Constructs a BoxAPIException that contains detailed message for underlying exception. From 0b15f6edc9f61cdf28389bf22c962f73cc6a20d9 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 9 Apr 2018 11:23:20 -0700 Subject: [PATCH 10/18] this time for real --- src/main/java/com/box/sdk/BoxAPIResponseException.java | 2 +- src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 55255b888..a11ea6820 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -14,7 +14,7 @@ public class BoxAPIResponseException extends BoxAPIException{ private BoxAPIResponse responseObj; - private String message; + public String message; /** * Constructs a BoxAPIException that contains detailed message for underlying exception. diff --git a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java index ca2d57ec1..27d696a5d 100644 --- a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java @@ -63,7 +63,7 @@ public void testAPIResponseExceptionReturnsCorrectErrorMessage() throws Malforme BoxJSONResponse response = (BoxJSONResponse) request.send(); } catch (BoxAPIResponseException e) { Assert.assertEquals(409, e.responseCode); - Assert.assertEquals("The API returned an error code [409 | 5678] item_name_in_use - Item with the same name already exists", e.message); + Assert.assertEquals("The API returned an error code [409 | 5678] item_name_in_use - Item with the same name already exists", e.message); return; } From 41475353e046400f8960f345e619dec37a9cb15a Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 9 Apr 2018 11:25:40 -0700 Subject: [PATCH 11/18] removed unncessary newline --- src/main/java/com/box/sdk/BoxAPIResponse.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index 454b4dd71..2e4a8338d 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -72,8 +72,7 @@ public BoxAPIResponse(int responseCode, Map headers) { public BoxAPIResponse(HttpURLConnection connection) { this.connection = connection; this.inputStream = null; - - + try { this.responseCode = this.connection.getResponseCode(); } catch (IOException e) { From 8a8039d7ae898235cb12e4fd64f03c6cc27d9fc2 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 9 Apr 2018 13:11:20 -0700 Subject: [PATCH 12/18] styling --- src/main/java/com/box/sdk/BoxAPIResponse.java | 2 +- src/main/java/com/box/sdk/BoxAPIResponseException.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index 2e4a8338d..b265fc770 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -72,7 +72,7 @@ public BoxAPIResponse(int responseCode, Map headers) { public BoxAPIResponse(HttpURLConnection connection) { this.connection = connection; this.inputStream = null; - + try { this.responseCode = this.connection.getResponseCode(); } catch (IOException e) { diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index a11ea6820..6bc1de1b1 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -1,8 +1,6 @@ package com.box.sdk; import com.eclipsesource.json.JsonObject; - -import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -11,7 +9,7 @@ /** * Thrown to indicate than an error occured while returning with a response from the Box API. */ -public class BoxAPIResponseException extends BoxAPIException{ +public class BoxAPIResponseException extends BoxAPIException { private BoxAPIResponse responseObj; public String message; From 8c285bbef15e0ee2e6e9d21f815edda509054ba3 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Thu, 26 Apr 2018 14:54:18 -0700 Subject: [PATCH 13/18] removed link from return tag --- .../com/box/sdk/BoxAPIResponseException.java | 94 ++++++++++--------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 6bc1de1b1..ea4bd7ed3 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -1,9 +1,10 @@ package com.box.sdk; import com.eclipsesource.json.JsonObject; -import java.util.ArrayList; + import java.util.HashMap; import java.util.List; +import java.util.ArrayList; import java.util.Map; /** @@ -11,50 +12,51 @@ */ public class BoxAPIResponseException extends BoxAPIException { - private BoxAPIResponse responseObj; public String message; - - /** - * Constructs a BoxAPIException that contains detailed message for underlying exception. - * @param message a message explaining why the error occurred. - * @param responseObj a response object from the server. - * @return a {@link BoxAPIResponseException} containing a custom error message that includes a request id, status - * code, and message from the server. - */ - public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { - super(message, responseObj.getResponseCode(), responseObj.bodyToString()); - String requestId = ""; - String apiMessage = ""; - JsonObject responseJSON = null; - this.responseObj = responseObj; - - Map> responseHeaders = new HashMap>(); - for(String headerKey : responseObj.getHeaders().keySet()) { - List headerValues = new ArrayList(); - headerValues.add(responseObj.getHeaderField(headerKey)); - responseHeaders.put(headerKey, headerValues); - } - this.headers = responseHeaders; - - if(responseObj.bodyToString()!=null && !responseObj.bodyToString().equals("")) { - responseJSON = JsonObject.readFrom(responseObj.bodyToString()); - - if(responseObj.bodyToString()!=null && responseJSON.get("request_id")!=null) { - requestId = " | " + responseJSON.get("request_id").asString(); - } - - if(responseObj.bodyToString()!=null && responseJSON.get("code")!=null) { - apiMessage += " " + responseJSON.get("code").asString(); - } - - if(responseObj.bodyToString()!=null && responseJSON.get("message")!=null) { - apiMessage += " - " + responseJSON.get("message").asString(); - } - - this.message = message + " [" + responseObj.getResponseCode() + requestId + "]" + apiMessage; - - } else { - this.message = message + " [" + responseObj.getResponseCode() + "]"; - } - } + private BoxAPIResponse responseObj; + + /** + * Constructs a BoxAPIException that contains detailed message for underlying exception. + * + * @param message a message explaining why the error occurred. + * @param responseObj a response object from the server. + * @return an exception containing a custom error message that includes a request id, status + * code, and message from the server. + */ + public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { + super(message, responseObj.getResponseCode(), responseObj.bodyToString()); + String requestId = ""; + String apiMessage = ""; + JsonObject responseJSON = null; + this.responseObj = responseObj; + + Map> responseHeaders = new HashMap>(); + for (String headerKey : responseObj.getHeaders().keySet()) { + List headerValues = new ArrayList(); + headerValues.add(responseObj.getHeaderField(headerKey)); + responseHeaders.put(headerKey, headerValues); + } + this.headers = responseHeaders; + + if (responseObj.bodyToString() != null && !responseObj.bodyToString().equals("")) { + responseJSON = JsonObject.readFrom(responseObj.bodyToString()); + + if (responseObj.bodyToString() != null && responseJSON.get("request_id") != null) { + requestId = " | " + responseJSON.get("request_id").asString(); + } + + if (responseObj.bodyToString() != null && responseJSON.get("code") != null) { + apiMessage += " " + responseJSON.get("code").asString(); + } + + if (responseObj.bodyToString() != null && responseJSON.get("message") != null) { + apiMessage += " - " + responseJSON.get("message").asString(); + } + + this.message = message + " [" + responseObj.getResponseCode() + requestId + "]" + apiMessage; + + } else { + this.message = message + " [" + responseObj.getResponseCode() + "]"; + } + } } From 1c996ce6a8c7e03288b51a04e021f6ca10efa674 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Thu, 26 Apr 2018 17:04:22 -0700 Subject: [PATCH 14/18] removed return tag for constructor --- src/main/java/com/box/sdk/BoxAPIResponseException.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index ea4bd7ed3..8a512b491 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -20,8 +20,6 @@ public class BoxAPIResponseException extends BoxAPIException { * * @param message a message explaining why the error occurred. * @param responseObj a response object from the server. - * @return an exception containing a custom error message that includes a request id, status - * code, and message from the server. */ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { super(message, responseObj.getResponseCode(), responseObj.bodyToString()); From 6c6868844a83a7fc04dc981dadfdc14c12d33ea4 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 30 Apr 2018 11:06:41 -0700 Subject: [PATCH 15/18] fixing lint errors --- src/main/java/com/box/sdk/BoxAPIResponse.java | 4 ++-- src/main/java/com/box/sdk/BoxAPIResponseException.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index b265fc770..e9ae0f8be 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -80,7 +80,7 @@ public BoxAPIResponse(HttpURLConnection connection) { } Map responseHeaders = new HashMap(); - for(String headerKey : connection.getHeaderFields().keySet()) { + for (String headerKey : connection.getHeaderFields().keySet()) { responseHeaders.put(headerKey, connection.getHeaderField(headerKey)); } this.headers = responseHeaders; @@ -201,7 +201,7 @@ public void disconnect() { /** * - * @return + * @return A Map containg headers on this Box API Response. */ public Map getHeaders() { return this.headers; diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 8a512b491..59b363c0b 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -2,9 +2,9 @@ import com.eclipsesource.json.JsonObject; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.ArrayList; import java.util.Map; /** @@ -12,7 +12,7 @@ */ public class BoxAPIResponseException extends BoxAPIException { - public String message; + public String message; private BoxAPIResponse responseObj; /** From 1b42984c45178f4c343e2ef852b2a49481207390 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 30 Apr 2018 13:53:12 -0700 Subject: [PATCH 16/18] fixing lint errors --- .../java/com/box/sdk/BoxAPIException.java | 30 +++++++++++++++++-- .../com/box/sdk/BoxAPIResponseException.java | 3 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIException.java b/src/main/java/com/box/sdk/BoxAPIException.java index f1ee46624..4eadc588e 100644 --- a/src/main/java/com/box/sdk/BoxAPIException.java +++ b/src/main/java/com/box/sdk/BoxAPIException.java @@ -10,9 +10,9 @@ public class BoxAPIException extends RuntimeException { private static final long serialVersionUID = 1L; - protected int responseCode; - protected String response; - protected Map> headers; + private int responseCode; + private String response; + private Map> headers; /** * Constructs a BoxAPIException with a specified message. @@ -131,4 +131,28 @@ public Map> getHeaders() { return Collections.emptyMap(); } } + + /** + * Sets the response code returned by the server. + * @param responseCode the response code returned by the server. + */ + protected void setResponseCode(int responseCode) { + this.responseCode = responseCode; + } + + /** + * Sets the response returned by ther server. + * @param response the response returned by the server. + */ + protected void setResponse(String response) { + this.response = response; + } + + /** + * Sets the response headers. + * @param headers headers to set. + */ + protected void setHeaders(Map> headers) { + this.headers = headers; + } } diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 59b363c0b..83d734326 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -34,7 +34,8 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { headerValues.add(responseObj.getHeaderField(headerKey)); responseHeaders.put(headerKey, headerValues); } - this.headers = responseHeaders; + + this.setHeaders(responseHeaders); if (responseObj.bodyToString() != null && !responseObj.bodyToString().equals("")) { responseJSON = JsonObject.readFrom(responseObj.bodyToString()); From d37de4238b89b2d997c08ee83469f7e7b12ceaab Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 30 Apr 2018 14:00:09 -0700 Subject: [PATCH 17/18] fix lint errors --- .../java/com/box/sdk/BoxAPIResponseException.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 83d734326..2ac40bc5e 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -12,7 +12,7 @@ */ public class BoxAPIResponseException extends BoxAPIException { - public String message; + private String message; private BoxAPIResponse responseObj; /** @@ -52,10 +52,18 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { apiMessage += " - " + responseJSON.get("message").asString(); } - this.message = message + " [" + responseObj.getResponseCode() + requestId + "]" + apiMessage; + this.setMessage(message + " [" + responseObj.getResponseCode() + requestId + "]" + apiMessage); } else { - this.message = message + " [" + responseObj.getResponseCode() + "]"; + this.setMessage(message + " [" + responseObj.getResponseCode() + "]"); } } + + /** + * The message to return for the API exception. + * @param message the constructed for the API exception. + */ + protected void setMessage(String message) { + this.message = message; + } } From 7d1e11a9a911ee64c03e8784e494401b77762b44 Mon Sep 17 00:00:00 2001 From: Cary Cheng Date: Mon, 30 Apr 2018 14:50:26 -0700 Subject: [PATCH 18/18] fixed ALL lint errors --- .../com/box/sdk/BoxAPIResponseException.java | 14 +- .../box/sdk/BoxAPIResponseExceptionTest.java | 227 +++++++++--------- 2 files changed, 125 insertions(+), 116 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIResponseException.java b/src/main/java/com/box/sdk/BoxAPIResponseException.java index 2ac40bc5e..4d461f05f 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponseException.java +++ b/src/main/java/com/box/sdk/BoxAPIResponseException.java @@ -1,12 +1,14 @@ package com.box.sdk; -import com.eclipsesource.json.JsonObject; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import com.eclipsesource.json.JsonObject; + + + /** * Thrown to indicate than an error occured while returning with a response from the Box API. */ @@ -66,4 +68,12 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) { protected void setMessage(String message) { this.message = message; } + + /** + * + * @return The constructed message for the API exception. + */ + public String getMessage() { + return this.message; + } } diff --git a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java index 27d696a5d..1478fe8be 100644 --- a/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java @@ -8,7 +8,6 @@ import static org.mockito.Mockito.*; import com.eclipsesource.json.JsonObject; -import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -22,117 +21,117 @@ * */ public class BoxAPIResponseExceptionTest { - @Rule - public WireMockRule wireMockRule = new WireMockRule(53620); - - @Test - @Category(UnitTest.class) - public void testAPIResponseExceptionReturnsCorrectErrorMessage() throws MalformedURLException{ - BoxAPIConnection api = new BoxAPIConnection(""); - - final JsonObject fakeJSONResponse = JsonObject.readFrom("{\n" - + " \"type\": \"error\",\n" - + " \"status\": \"409\",\n" - + " \"code\": \"item_name_in_use\",\n" - + " \"context_info\": {\n" - + " \"conflicts\": [\n" - + " {\n" - + " \"type\": \"folder\",\n" - + " \"id\": \"12345\",\n" - + " \"sequence_id\": \"1\",\n" - + " \"etag\": \"1\",\n" - + " \"name\": \"Helpful things\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"help_url\": \"http://developers.box.com/docs/#errors\",\n" - + " \"message\": \"Item with the same name already exists\",\n" - + " \"request_id\": \"5678\"\n" - + " }"); - - stubFor(post(urlEqualTo("/folders")) - .willReturn(aResponse() - .withStatus(409) - .withHeader("Content-Type", "application/json") - .withBody(fakeJSONResponse.toString()))); - - URL url = new URL("http://localhost:53620/folders"); - BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); - - try { - BoxJSONResponse response = (BoxJSONResponse) request.send(); - } catch (BoxAPIResponseException e) { - Assert.assertEquals(409, e.responseCode); - Assert.assertEquals("The API returned an error code [409 | 5678] item_name_in_use - Item with the same name already exists", e.message); - return; - } - - Assert.fail("Never threw a BoxAPIResponseException"); - } - - @Test - @Category(UnitTest.class) - public void testAPIResponseExceptionMissingFieldsReturnsCorrectErrorMessage() throws MalformedURLException{ - BoxAPIConnection api = new BoxAPIConnection(""); - - final JsonObject fakeJSONResponse = JsonObject.readFrom("{\n" - + " \"type\": \"error\",\n" - + " \"status\": \"409\",\n" - + " \"context_info\": {\n" - + " \"conflicts\": [\n" - + " {\n" - + " \"type\": \"folder\",\n" - + " \"id\": \"12345\",\n" - + " \"sequence_id\": \"1\",\n" - + " \"etag\": \"1\",\n" - + " \"name\": \"Helpful things\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"help_url\": \"http://developers.box.com/docs/#errors\"\n" - + " }"); - - stubFor(post(urlEqualTo("/folders")) - .willReturn(aResponse() - .withStatus(409) - .withHeader("Content-Type", "application/json") - .withBody(fakeJSONResponse.toString()))); - - URL url = new URL("http://localhost:53620/folders"); - BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); - - try { - BoxJSONResponse response = (BoxJSONResponse) request.send(); - } catch (BoxAPIResponseException e) { - Assert.assertEquals(409, e.responseCode); - Assert.assertEquals("The API returned an error code [409]", e.message); - return; - } - - Assert.fail("Never threw a BoxAPIResponseException"); - } - - @Test - @Category(UnitTest.class) - public void testAPIResponseExceptionMissingBodyReturnsCorrectErrorMessage() throws MalformedURLException{ - BoxAPIConnection api = new BoxAPIConnection(""); - - stubFor(post(urlEqualTo("/folders")) - .willReturn(aResponse() - .withStatus(403))); - - URL url = new URL("http://localhost:53620/folders"); - BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); - - try { - BoxJSONResponse response = (BoxJSONResponse) request.send(); - } catch (BoxAPIResponseException e) { - Assert.assertEquals(403, e.responseCode); - Assert.assertEquals("", e.response); - Assert.assertEquals("The API returned an error code [403]", e.message); - return; - } - - Assert.fail("Never threw a BoxAPIResponseException"); - } + @Rule + public WireMockRule wireMockRule = new WireMockRule(53620); + + @Test + @Category(UnitTest.class) + public void testAPIResponseExceptionReturnsCorrectErrorMessage() throws MalformedURLException { + BoxAPIConnection api = new BoxAPIConnection(""); + + final JsonObject fakeJSONResponse = JsonObject.readFrom("{\n" + + " \"type\": \"error\",\n" + + " \"status\": \"409\",\n" + + " \"code\": \"item_name_in_use\",\n" + + " \"context_info\": {\n" + + " \"conflicts\": [\n" + + " {\n" + + " \"type\": \"folder\",\n" + + " \"id\": \"12345\",\n" + + " \"sequence_id\": \"1\",\n" + + " \"etag\": \"1\",\n" + + " \"name\": \"Helpful things\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"help_url\": \"http://developers.box.com/docs/#errors\",\n" + + " \"message\": \"Item with the same name already exists\",\n" + + " \"request_id\": \"5678\"\n" + + " }"); + + stubFor(post(urlEqualTo("/folders")) + .willReturn(aResponse() + .withStatus(409) + .withHeader("Content-Type", "application/json") + .withBody(fakeJSONResponse.toString()))); + + URL url = new URL("http://localhost:53620/folders"); + BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); + + try { + BoxJSONResponse response = (BoxJSONResponse) request.send(); + } catch (BoxAPIResponseException e) { + Assert.assertEquals(409, e.getResponseCode()); + Assert.assertEquals("The API returned an error code [409 | 5678] item_name_in_use - " + + "Item with the same name already exists", e.getMessage()); + return; + } + + Assert.fail("Never threw a BoxAPIResponseException"); + } + + @Test + @Category(UnitTest.class) + public void testAPIResponseExceptionMissingFieldsReturnsCorrectErrorMessage() throws MalformedURLException { + BoxAPIConnection api = new BoxAPIConnection(""); + final JsonObject fakeJSONResponse = JsonObject.readFrom("{\n" + + " \"type\": \"error\",\n" + + " \"status\": \"409\",\n" + + " \"context_info\": {\n" + + " \"conflicts\": [\n" + + " {\n" + + " \"type\": \"folder\",\n" + + " \"id\": \"12345\",\n" + + " \"sequence_id\": \"1\",\n" + + " \"etag\": \"1\",\n" + + " \"name\": \"Helpful things\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"help_url\": \"http://developers.box.com/docs/#errors\"\n" + + " }"); + + stubFor(post(urlEqualTo("/folders")) + .willReturn(aResponse() + .withStatus(409) + .withHeader("Content-Type", "application/json") + .withBody(fakeJSONResponse.toString()))); + + URL url = new URL("http://localhost:53620/folders"); + BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); + + try { + BoxJSONResponse response = (BoxJSONResponse) request.send(); + } catch (BoxAPIResponseException e) { + Assert.assertEquals(409, e.getResponseCode()); + Assert.assertEquals("The API returned an error code [409]", e.getMessage()); + return; + } + + Assert.fail("Never threw a BoxAPIResponseException"); + } + + @Test + @Category(UnitTest.class) + public void testAPIResponseExceptionMissingBodyReturnsCorrectErrorMessage() throws MalformedURLException { + BoxAPIConnection api = new BoxAPIConnection(""); + + stubFor(post(urlEqualTo("/folders")) + .willReturn(aResponse() + .withStatus(403))); + + URL url = new URL("http://localhost:53620/folders"); + BoxAPIRequest request = new BoxAPIRequest(api, url, "POST"); + + try { + BoxJSONResponse response = (BoxJSONResponse) request.send(); + } catch (BoxAPIResponseException e) { + Assert.assertEquals(403, e.getResponseCode()); + Assert.assertEquals("", e.getResponse()); + Assert.assertEquals("The API returned an error code [403]", e.getMessage()); + return; + } + + Assert.fail("Never threw a BoxAPIResponseException"); + } }