Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add additional API exception types for statuses 501 (XeroNotImplement… #273

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/xero/api/XeroApiExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -261,3 +264,4 @@ public void execute(HttpResponseException e, ApiClient apiClient)
}

}

37 changes: 37 additions & 0 deletions src/main/java/com/xero/api/XeroNotAvailableException.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
37 changes: 37 additions & 0 deletions src/main/java/com/xero/api/XeroNotImplementedException.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
29 changes: 29 additions & 0 deletions src/test/java/com/xero/api/XeroExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -212,3 +240,4 @@ public void testXeroApiException() {
}

}