From 94bc83741189fb9cd87ac2a39779aee04624f1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Ca=C3=B1izares?= Date: Fri, 16 Dec 2016 11:45:10 +0100 Subject: [PATCH] Added headers info within the RestException class --- .../java/net/rcarz/jiraclient/RestClient.java | 22 +++++++++---------- .../net/rcarz/jiraclient/RestException.java | 11 +++++++--- .../rcarz/jiraclient/agile/BoardTest.groovy | 4 ++-- .../rcarz/jiraclient/agile/EpicTest.groovy | 2 +- .../rcarz/jiraclient/agile/IssueTest.groovy | 4 ++-- .../rcarz/jiraclient/agile/SprintTest.groovy | 4 ++-- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/rcarz/jiraclient/RestClient.java b/src/main/java/net/rcarz/jiraclient/RestClient.java index 80fefd7..c96255b 100644 --- a/src/main/java/net/rcarz/jiraclient/RestClient.java +++ b/src/main/java/net/rcarz/jiraclient/RestClient.java @@ -19,16 +19,6 @@ package net.rcarz.jiraclient; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Map; - import net.sf.json.JSON; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; @@ -53,6 +43,16 @@ import org.apache.http.entity.mime.content.ByteArrayBody; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.InputStreamBody; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; + /** * A simple REST client that speaks JSON. */ @@ -160,7 +160,7 @@ public class RestClient { StatusLine sl = resp.getStatusLine(); if (sl.getStatusCode() >= 300) - throw new RestException(sl.getReasonPhrase(), sl.getStatusCode(), result.toString()); + throw new RestException(sl.getReasonPhrase(), sl.getStatusCode(), result.toString(), resp.getAllHeaders()); return result.length() > 0 ? JSONSerializer.toJSON(result.toString()): null; } diff --git a/src/main/java/net/rcarz/jiraclient/RestException.java b/src/main/java/net/rcarz/jiraclient/RestException.java index fa234d9..76ca221 100644 --- a/src/main/java/net/rcarz/jiraclient/RestException.java +++ b/src/main/java/net/rcarz/jiraclient/RestException.java @@ -19,7 +19,7 @@ package net.rcarz.jiraclient; -import java.lang.Throwable; +import org.apache.http.Header; /** * An exception for JIRA REST errors. @@ -28,12 +28,14 @@ public class RestException extends Exception { private int status; private String result; + private Header[] headers; - public RestException(String msg, int status, String result) { + public RestException(String msg, int status, String result, Header[] headers) { super(msg); this.status = status; this.result = result; + this.headers = headers; } public int getHttpStatusCode() { @@ -44,8 +46,11 @@ public class RestException extends Exception { return result; } + public Header[] getHeaders() { + return headers; + } + public String getMessage() { return String.format("%s %s: %s", Integer.toString(status), super.getMessage(), result); } } - diff --git a/src/test/groovy/net/rcarz/jiraclient/agile/BoardTest.groovy b/src/test/groovy/net/rcarz/jiraclient/agile/BoardTest.groovy index eaba815..dbd7837 100644 --- a/src/test/groovy/net/rcarz/jiraclient/agile/BoardTest.groovy +++ b/src/test/groovy/net/rcarz/jiraclient/agile/BoardTest.groovy @@ -39,7 +39,7 @@ class BoardTest extends AbstractResourceTest { @Test void "Given a RestClient, when calling getAll() and use doesn't have access, then throws an 401 error."() { - RestException unauthorized = new RestException("Do not have access", 401, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 401, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "board")) .thenThrow(unauthorized) @@ -62,7 +62,7 @@ class BoardTest extends AbstractResourceTest { @Test void "Given a RestClient, when calling getBoard(666), then throws an 404 error."() { - RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/666")) .thenThrow(unauthorized) diff --git a/src/test/groovy/net/rcarz/jiraclient/agile/EpicTest.groovy b/src/test/groovy/net/rcarz/jiraclient/agile/EpicTest.groovy index 9034615..5563a0f 100644 --- a/src/test/groovy/net/rcarz/jiraclient/agile/EpicTest.groovy +++ b/src/test/groovy/net/rcarz/jiraclient/agile/EpicTest.groovy @@ -37,7 +37,7 @@ class EpicTest extends AbstractResourceTest { @Test void "Given an invalid epic ID, when calling getEpic(666), then throws an 404 error."() { - RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "epic/666")) .thenThrow(unauthorized) diff --git a/src/test/groovy/net/rcarz/jiraclient/agile/IssueTest.groovy b/src/test/groovy/net/rcarz/jiraclient/agile/IssueTest.groovy index 65f232f..217ed1c 100644 --- a/src/test/groovy/net/rcarz/jiraclient/agile/IssueTest.groovy +++ b/src/test/groovy/net/rcarz/jiraclient/agile/IssueTest.groovy @@ -32,7 +32,7 @@ class IssueTest extends AbstractResourceTest { @Test void "Given an invalid issue ID, when calling getIssue(666), then throws an 404 error."() { - RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "issue/666")) .thenThrow(unauthorized) @@ -55,7 +55,7 @@ class IssueTest extends AbstractResourceTest { @Test void "Given an invalid issue Key, when calling getIssue('HSP-2'), then throws an 404 error."() { - RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "issue/HSP-2")) .thenThrow(unauthorized) diff --git a/src/test/groovy/net/rcarz/jiraclient/agile/SprintTest.groovy b/src/test/groovy/net/rcarz/jiraclient/agile/SprintTest.groovy index 330ee22..693cd40 100644 --- a/src/test/groovy/net/rcarz/jiraclient/agile/SprintTest.groovy +++ b/src/test/groovy/net/rcarz/jiraclient/agile/SprintTest.groovy @@ -39,7 +39,7 @@ class SprintTest extends AbstractResourceTest { @Test void "Given a RestClient, when calling getAll() and use doesn't have access, then throws an 401 error."() { - RestException unauthorized = new RestException("Do not have access", 401, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 401, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "board/" + JSONResources.SPRINT_ORIGIN_BOARD_ID + "/sprint")) .thenThrow(unauthorized) @@ -62,7 +62,7 @@ class SprintTest extends AbstractResourceTest { @Test void "Given a RestClient, when calling getSprint(666), then throws an 404 error."() { - RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized") + RestException unauthorized = new RestException("Do not have access", 404, "Unauthorized", []) RestClient mockRestClient = "given a REST Client"() when(mockRestClient.get(AgileResource.RESOURCE_URI + "sprint/666")) .thenThrow(unauthorized)