1
0
Fork 0

Merge pull request #192 from viktorKhan/master

Added headers info within the RestException class
master
Bob Carroll 2017-07-02 12:29:27 -07:00 committed by GitHub
commit c5c2864195
6 changed files with 26 additions and 21 deletions

View File

@ -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;
@ -54,6 +44,16 @@ import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.util.EntityUtils;
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.
*/
@ -168,7 +168,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;
}

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)