diff --git a/README.md b/README.md index 64b657ad..95c8de94 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ To address this we've refactored exception handling and we are deprecating the g | 429 | XeroDailyRateLimitException | The API daily rate limit for your organisation/application pairing has been exceeded. | | 429 | XeroMinuteRateLimitException | The API minute rate limit for your organisation/application pairing has been exceeded. | | 500 | XeroServerErrorException | An unhandled error with the Xero API | +| 501 | XeroNotImplementedException | Method not implemented for the organisation | +| 503 | XeroNotAvailableException | The organisation temporarily cannot be connected to or API is currently unavailable – typically due to a scheduled outage | Below is a try/catch example diff --git a/src/main/java/com/xero/api/XeroApiExceptionHandler.java b/src/main/java/com/xero/api/XeroApiExceptionHandler.java index 99cd323f..828a3682 100644 --- a/src/main/java/com/xero/api/XeroApiExceptionHandler.java +++ b/src/main/java/com/xero/api/XeroApiExceptionHandler.java @@ -210,10 +210,13 @@ public void execute(HttpResponseException e) { String message = "An error occurred in Xero. Check the API Status page http://status.developer.xero.com for current service status."; throw new XeroServerErrorException(statusCode, message, e); - } else if (statusCode > 500) { - String message = "Internal Server Error"; - throw new XeroServerErrorException(statusCode, message, e); - + } else if (statusCode == 501) { + String message = "The method you have called has not been implemented"; + throw new XeroNotImplementedException(statusCode, message, e); + + } else if (statusCode == 503) { + throw new XeroNotAvailableException(statusCode, e.getStatusMessage(), e); + } else { throw new XeroApiException(statusCode, e.getStatusMessage(), e); } @@ -261,3 +264,4 @@ public void execute(HttpResponseException e, ApiClient apiClient) } } + diff --git a/src/main/java/com/xero/api/XeroNotAvailableException.java b/src/main/java/com/xero/api/XeroNotAvailableException.java new file mode 100644 index 00000000..b01b74d6 --- /dev/null +++ b/src/main/java/com/xero/api/XeroNotAvailableException.java @@ -0,0 +1,37 @@ +package com.xero.api; + +/** handle not available exception */ +public class XeroNotAvailableException extends XeroException { + + private static final long serialVersionUID = 1L; + private int statusCode = 0; + private String message; + + /** XeroNotAvailableException + * @param statusCode Integer the server status code returned. + * @param message String with details about the exception + * @param e Exception object with details about the original exception + */ + public XeroNotAvailableException(int statusCode, String message, Exception e) { + super(statusCode + " : " + message, e); + this.statusCode = statusCode; + this.message = message; + } + + /** + * get status code + * @return statusCode + **/ + public int getStatusCode() { + return statusCode; + } + + /** + * get message + * @return message + **/ + public String getMessage() { + return message; + } + +} diff --git a/src/main/java/com/xero/api/XeroNotImplementedException.java b/src/main/java/com/xero/api/XeroNotImplementedException.java new file mode 100644 index 00000000..f0c94cc1 --- /dev/null +++ b/src/main/java/com/xero/api/XeroNotImplementedException.java @@ -0,0 +1,37 @@ +package com.xero.api; + +/** handle not available exception */ +public class XeroNotImplementedException extends XeroException { + + private static final long serialVersionUID = 1L; + private int statusCode = 0; + private String message; + + /** XeroNotImplementedException + * @param statusCode Integer the server status code returned. + * @param message String with details about the exception + * @param e Exception object with details about the original exception + */ + public XeroNotImplementedException(int statusCode, String message, Exception e) { + super(statusCode + " : " + message, e); + this.statusCode = statusCode; + this.message = message; + } + + /** + * get status code + * @return statusCode + **/ + public int getStatusCode() { + return statusCode; + } + + /** + * get message + * @return message + **/ + public String getMessage() { + return message; + } + +} diff --git a/src/test/java/com/xero/api/XeroExceptionsTest.java b/src/test/java/com/xero/api/XeroExceptionsTest.java index 090138cc..13c34ac0 100644 --- a/src/test/java/com/xero/api/XeroExceptionsTest.java +++ b/src/test/java/com/xero/api/XeroExceptionsTest.java @@ -196,6 +196,34 @@ public void testXeroServerErrorException() { xeroApiExceptionHandler.execute(httpResponseException); } + @Test + public void testXeroNotImplementedException() { + int statusCode = 501; + String message = "The method you have called has not been implemented"; + + // XeroNotImplementedException extends XeroException so we can catch either + expectedException.expect(XeroException.class); + expectedException.expect(XeroNotImplementedException.class); + expectedException.expectMessage(message); + + when(httpResponseException.getStatusCode()).thenReturn(statusCode); + xeroApiExceptionHandler.execute(httpResponseException); + } + + + @Test + public void testXeroNotAvailableException() { + // 3 different messages can be returned with status code 503 so we won't check them + int statusCode = 503; + + // XeroNotAvailableException extends XeroException so we can catch either + expectedException.expect(XeroException.class); + expectedException.expect(XeroNotAvailableException.class); + + when(httpResponseException.getStatusCode()).thenReturn(statusCode); + xeroApiExceptionHandler.execute(httpResponseException); + } + @Test public void testXeroApiException() { int statusCode = 1; @@ -212,3 +240,4 @@ public void testXeroApiException() { } } +