Skip to content

Commit 8009f70

Browse files
authored
Merge pull request #273 from evelinasmit/xero-exception-handler
add additional API exception types for statuses 501 (XeroNotImplement…
2 parents 0ead3dc + 28d0f7c commit 8009f70

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ To address this we've refactored exception handling and we are deprecating the g
6565
| 429 | XeroDailyRateLimitException | The API daily rate limit for your organisation/application pairing has been exceeded. |
6666
| 429 | XeroMinuteRateLimitException | The API minute rate limit for your organisation/application pairing has been exceeded. |
6767
| 500 | XeroServerErrorException | An unhandled error with the Xero API |
68+
| 501 | XeroNotImplementedException | Method not implemented for the organisation |
69+
| 503 | XeroNotAvailableException | The organisation temporarily cannot be connected to or API is currently unavailable – typically due to a scheduled outage |
6870

6971
Below is a try/catch example
7072

src/main/java/com/xero/api/XeroApiExceptionHandler.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,13 @@ public void execute(HttpResponseException e) {
210210
String message = "An error occurred in Xero. Check the API Status page http://status.developer.xero.com for current service status.";
211211
throw new XeroServerErrorException(statusCode, message, e);
212212

213-
} else if (statusCode > 500) {
214-
String message = "Internal Server Error";
215-
throw new XeroServerErrorException(statusCode, message, e);
216-
213+
} else if (statusCode == 501) {
214+
String message = "The method you have called has not been implemented";
215+
throw new XeroNotImplementedException(statusCode, message, e);
216+
217+
} else if (statusCode == 503) {
218+
throw new XeroNotAvailableException(statusCode, e.getStatusMessage(), e);
219+
217220
} else {
218221
throw new XeroApiException(statusCode, e.getStatusMessage(), e);
219222
}
@@ -261,3 +264,4 @@ public void execute(HttpResponseException e, ApiClient apiClient)
261264
}
262265

263266
}
267+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.xero.api;
2+
3+
/** handle not available exception */
4+
public class XeroNotAvailableException extends XeroException {
5+
6+
private static final long serialVersionUID = 1L;
7+
private int statusCode = 0;
8+
private String message;
9+
10+
/** XeroNotAvailableException
11+
* @param statusCode Integer the server status code returned.
12+
* @param message String with details about the exception
13+
* @param e Exception object with details about the original exception
14+
*/
15+
public XeroNotAvailableException(int statusCode, String message, Exception e) {
16+
super(statusCode + " : " + message, e);
17+
this.statusCode = statusCode;
18+
this.message = message;
19+
}
20+
21+
/**
22+
* get status code
23+
* @return statusCode
24+
**/
25+
public int getStatusCode() {
26+
return statusCode;
27+
}
28+
29+
/**
30+
* get message
31+
* @return message
32+
**/
33+
public String getMessage() {
34+
return message;
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.xero.api;
2+
3+
/** handle not available exception */
4+
public class XeroNotImplementedException extends XeroException {
5+
6+
private static final long serialVersionUID = 1L;
7+
private int statusCode = 0;
8+
private String message;
9+
10+
/** XeroNotImplementedException
11+
* @param statusCode Integer the server status code returned.
12+
* @param message String with details about the exception
13+
* @param e Exception object with details about the original exception
14+
*/
15+
public XeroNotImplementedException(int statusCode, String message, Exception e) {
16+
super(statusCode + " : " + message, e);
17+
this.statusCode = statusCode;
18+
this.message = message;
19+
}
20+
21+
/**
22+
* get status code
23+
* @return statusCode
24+
**/
25+
public int getStatusCode() {
26+
return statusCode;
27+
}
28+
29+
/**
30+
* get message
31+
* @return message
32+
**/
33+
public String getMessage() {
34+
return message;
35+
}
36+
37+
}

src/test/java/com/xero/api/XeroExceptionsTest.java

+29
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,34 @@ public void testXeroServerErrorException() {
196196
xeroApiExceptionHandler.execute(httpResponseException);
197197
}
198198

199+
@Test
200+
public void testXeroNotImplementedException() {
201+
int statusCode = 501;
202+
String message = "The method you have called has not been implemented";
203+
204+
// XeroNotImplementedException extends XeroException so we can catch either
205+
expectedException.expect(XeroException.class);
206+
expectedException.expect(XeroNotImplementedException.class);
207+
expectedException.expectMessage(message);
208+
209+
when(httpResponseException.getStatusCode()).thenReturn(statusCode);
210+
xeroApiExceptionHandler.execute(httpResponseException);
211+
}
212+
213+
214+
@Test
215+
public void testXeroNotAvailableException() {
216+
// 3 different messages can be returned with status code 503 so we won't check them
217+
int statusCode = 503;
218+
219+
// XeroNotAvailableException extends XeroException so we can catch either
220+
expectedException.expect(XeroException.class);
221+
expectedException.expect(XeroNotAvailableException.class);
222+
223+
when(httpResponseException.getStatusCode()).thenReturn(statusCode);
224+
xeroApiExceptionHandler.execute(httpResponseException);
225+
}
226+
199227
@Test
200228
public void testXeroApiException() {
201229
int statusCode = 1;
@@ -212,3 +240,4 @@ public void testXeroApiException() {
212240
}
213241

214242
}
243+

0 commit comments

Comments
 (0)