Skip to content

Commit c318c49

Browse files
authored
DX-1036 (#19)
* DX-1036 * Bubbled up the status code
1 parent c0fca47 commit c318c49

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

src/main/java/com/bandwidth/iris/sdk/IrisClient.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ private void initHttpClient(String userName, String password) {
5252
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
5353
}
5454

55-
private <T> T processResponse(String responseBody, Class<T> returnType) throws Exception {
56-
T parsedResponse = XmlUtils.fromXml(responseBody, returnType);
55+
private <T> T processResponse(IrisResponse response, Class<T> returnType) throws Exception {
56+
T parsedResponse = XmlUtils.fromXml(response.getResponseBody(), returnType);
5757
if (parsedResponse instanceof BaseResponse) {
5858
BaseResponse baseResponse = (BaseResponse) parsedResponse;
5959
if (baseResponse.getResponseStatus() != null) {
60-
throw new IrisClientException(baseResponse.getResponseStatus().getErrorCode(),
60+
throw new IrisClientException(response.getStatusCode(), baseResponse.getResponseStatus().getErrorCode(),
6161
baseResponse.getResponseStatus().getDescription());
6262
}
6363
}
@@ -66,15 +66,15 @@ private <T> T processResponse(String responseBody, Class<T> returnType) throws E
6666

6767
public <T> T get(String uri, Class<T> returnType) throws Exception {
6868
IrisResponse response = get(uri);
69-
return processResponse(response.getResponseBody(), returnType);
69+
return processResponse(response, returnType);
7070
}
7171

7272
public byte[] getFile(String uri) throws Exception {
7373
HttpGet get = new HttpGet(uri);
7474
HttpResponse response = httpClient.execute(get);
7575

7676
if (response.getStatusLine().getStatusCode() != 200) {
77-
throw new IrisClientException("Status code getting LOAS file: " + response.getStatusLine().getStatusCode());
77+
throw new IrisClientException( response.getStatusLine().getStatusCode(), "Status code getting LOAS file: " + response.getStatusLine().getStatusCode(), "");
7878
}
7979
return response.getEntity() != null ? EntityUtils.toByteArray(response.getEntity()) : new byte[]{};
8080
}
@@ -86,7 +86,7 @@ public IrisResponse get(String uri) throws Exception {
8686

8787
public <T> T post(String uri, BaseModel data, Class<T> returnType) throws Exception {
8888
IrisResponse response = post(uri, data);
89-
return processResponse(response.getResponseBody(), returnType);
89+
return processResponse(response, returnType);
9090
}
9191

9292
public IrisResponse post(String uri, BaseModel data) throws Exception {
@@ -104,7 +104,7 @@ public IrisResponse delete(String uri) throws Exception {
104104

105105
public <T> T put(String uri, BaseModel data, Class<T> returnType) throws Exception {
106106
IrisResponse response = put(uri, data);
107-
return processResponse(response.getResponseBody(), returnType);
107+
return processResponse(response, returnType);
108108
}
109109

110110
public IrisResponse put(String uri, BaseModel data) throws Exception {
@@ -170,7 +170,7 @@ protected IrisResponse executeRequest(HttpUriRequest request) throws Exception {
170170
irisResponse.setHeaders(headers);
171171

172172
if (!irisResponse.isOK()) {
173-
throw new IrisClientException(irisResponse.getResponseBody());
173+
throw new IrisClientException(irisResponse.getStatusCode(), "", irisResponse.getResponseBody());
174174
}
175175
return irisResponse;
176176
}
@@ -179,10 +179,10 @@ public String getIdFromLocationHeader(String locationHeader) {
179179
return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
180180
}
181181

182-
public void checkResponse(BaseResponse response) throws IrisClientException {
183-
if (response.getResponseStatus() != null) {
184-
throw new IrisClientException(response.getResponseStatus().getErrorCode(),
185-
response.getResponseStatus().getDescription());
182+
public void checkResponse( IrisResponse response, BaseResponse baseResponse ) throws IrisClientException{
183+
if(baseResponse.getResponseStatus() != null ){
184+
throw new IrisClientException(response.getStatusCode(),baseResponse.getResponseStatus().getErrorCode(),
185+
baseResponse.getResponseStatus().getDescription());
186186
}
187187
}
188188

src/main/java/com/bandwidth/iris/sdk/IrisClientException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
public class IrisClientException extends Exception {
44
private String errorCode;
55
private String description;
6+
private int statusCode;
67

78
public IrisClientException(Throwable e) {
89
super(e);
910
}
10-
11-
public IrisClientException(String message) {
12-
super(message);
13-
}
14-
15-
public IrisClientException(String errorCode, String description) {
11+
12+
public IrisClientException(int statusCode, String errorCode, String description) {
1613
super(description);
1714
this.errorCode = errorCode;
1815
this.description = description;
16+
this.statusCode = statusCode;
1917
}
2018

2119
public String getErrorCode() {
@@ -26,4 +24,6 @@ public String getDescription() {
2624
return description;
2725
}
2826

27+
public int getStatusCode() { return statusCode; }
28+
2929
}

src/main/java/com/bandwidth/iris/sdk/model/Reservation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static Reservation create(IrisClient client, Reservation reservation) thr
3838
if (response.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
3939
ReservationResponse reservationResponse =
4040
XmlUtils.fromXml(response.getResponseBody(), ReservationResponse.class);
41-
client.checkResponse(reservationResponse);
41+
client.checkResponse(response, reservationResponse);
4242
}
4343
return result;
4444
}

src/main/java/com/bandwidth/iris/sdk/model/Site.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static Site create(IrisClient client, Site site) throws Exception {
4040
public static List<Site> list(IrisClient client) throws Exception {
4141
SitesResponse sitesResponse = client
4242
.get(client.buildAccountModelUri(new String[] { IrisPath.SITES_URI_PATH }), SitesResponse.class);
43-
client.checkResponse(sitesResponse);
4443
List<Site> sites = sitesResponse.getSites();
4544
for (Site s : sites) {
4645
s.setClient(client);

src/main/java/com/bandwidth/iris/sdk/model/Tns.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static TelephoneNumberResponse checkStatus(IrisClient client, String tn)
4040
//Have to manually parse for this error because of how it's returned
4141
if (irisResponse.getResponseBody().indexOf("ErrorResponse") != -1) {
4242
ErrorResponse errorResponse = XmlUtils.fromXml(irisResponse.getResponseBody(), ErrorResponse.class);
43-
throw new IrisClientException(errorResponse.getIrisStatus().getCode(),
43+
throw new IrisClientException(irisResponse.getStatusCode(),errorResponse.getIrisStatus().getCode(),
4444
errorResponse.getIrisStatus().getDescription());
4545
} else {
4646
return XmlUtils.fromXml(irisResponse.getResponseBody(), TelephoneNumberResponse.class);

src/test/java/com/bandwidth/iris/sdk/AvailableNpaNxxTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,24 @@ public void testSearchNpaNxx() throws Exception {
2828
assertEquals(2, result.size());
2929
assertEquals("919", result.get(0).getNpa());
3030
}
31+
32+
@Test
33+
public void testIrisClientException() throws Exception {
34+
String url = "/v1.0/accounts/accountId/availableNpaNxx.*";
35+
stubFor(get(urlMatching(url))
36+
.willReturn(aResponse()
37+
.withStatus(400).withBody(IrisClientTestUtils.availableNpaNxxSearchResultExceptionXml)));
38+
39+
Map<String, Object> query = new HashMap<String, Object>();
40+
query.put("areaCode", "919");
41+
42+
43+
try {
44+
List<AvailableNpaNxx> result = AvailableNpaNxx.list(getDefaultClient(), query);
45+
} catch ( IrisClientException ex ){
46+
assertEquals(400, ex.getStatusCode());
47+
}
48+
49+
50+
}
3151
}

src/test/java/com/bandwidth/iris/sdk/IrisClientTestUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public class IrisClientTestUtils {
111111
*/
112112
public static String localAreaSearchResultXml = "<SearchResult><ResultCount>2</ResultCount><TelephoneNumberDetailList><TelephoneNumberDetail><City>JERSEY CITY</City><LATA>224</LATA><RateCenter>JERSEYCITY</RateCenter><State>NJ</State><TelephoneNumber>2012001555</TelephoneNumber></TelephoneNumberDetail><TelephoneNumberDetail><City>JERSEY CITY</City><LATA>224</LATA><RateCenter>JERSEYCITY</RateCenter><State>NJ</State><TelephoneNumber>123123123</TelephoneNumber></TelephoneNumberDetail></TelephoneNumberDetailList></SearchResult>";
113113
public static String availableNpaNxxSearchResultXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><SearchResultForAvailableNpaNxx><AvailableNpaNxxList><AvailableNpaNxx><City>RALEIGH</City><Npa>919</Npa><Nxx>555</Nxx><Quantity>52</Quantity><State>NC</State></AvailableNpaNxx><AvailableNpaNxx><City>CARY</City><Npa>919</Npa><Nxx>556</Nxx><Quantity>168</Quantity><State>NC</State></AvailableNpaNxx></AvailableNpaNxxList></SearchResultForAvailableNpaNxx>";
114+
public static String availableNpaNxxSearchResultExceptionXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><SearchResultForAvailableNpaNxx><ResponseStatus><ErrorCode>12016</ErrorCode><Description>Site '5001' does not exist</Description></ResponseStatus><AvailableNpaNxxList><AvailableNpaNxx><City>RALEIGH</City><Npa>919</Npa><Nxx>555</Nxx><Quantity>52</Quantity><State>NC</State></AvailableNpaNxx><AvailableNpaNxx><City>CARY</City><Npa>919</Npa><Nxx>556</Nxx><Quantity>168</Quantity><State>NC</State></AvailableNpaNxx></AvailableNpaNxxList></SearchResultForAvailableNpaNxx>";
114115
/**
115116
* Sites Xmls
116117
*/

0 commit comments

Comments
 (0)