Skip to content

Commit ca427c8

Browse files
authored
Merge pull request #234 from XeroAPI/sid-development
Merging 233 - Improve exception handling
2 parents f673c45 + 3ecfe1b commit ca427c8

22 files changed

Lines changed: 463 additions & 425 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>xero-java</artifactId>
66
<packaging>jar</packaging>
77
<name>xero-java</name>
8-
<version>4.3.1</version>
8+
<version>4.3.2</version>
99
<url>https://github.com/XeroAPI/Xero-Java</url>
1010
<description>This is the official Java SDK for Xero API</description>
1111
<licenses>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public XeroApiException(int responseCode) {
1919
this.responseCode = responseCode;
2020
}
2121

22-
public XeroApiException(int responseCode, String message) {
23-
super(responseCode + " response: " + message);
22+
public XeroApiException(int responseCode, String message, Exception e) {
23+
super(responseCode + " response: " + message, e);
2424
this.responseCode = responseCode;
2525
this.message = message;
2626
}
@@ -38,14 +38,14 @@ public XeroApiException(int responseCode, Map<String, String> map) {
3838
this.messageMap = map;
3939
}
4040

41-
public XeroApiException(int responseCode, Error error) {
42-
super(responseCode + " response: none");
41+
public XeroApiException(int responseCode, Error error, Exception e) {
42+
super(responseCode + " response: none", e);
4343
this.responseCode = responseCode;
4444
this.error = error;
4545
}
4646

47-
public XeroApiException(int responseCode, String message, Error error) {
48-
super(responseCode + " response: " + message);
47+
public XeroApiException(int responseCode, String message, Error error, Exception e) {
48+
super(responseCode + " response: " + message, e);
4949
this.responseCode = responseCode;
5050
this.message = message;
5151
this.error = error;

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

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,76 @@ public XeroApiExceptionHandler() {
2020

2121
// REFACTOR ERROR HANDLER
2222
// ACCOUNTING Validation Errors (400)
23-
public void validationError(String objectType, com.xero.models.accounting.Error error) {
24-
throw new XeroBadRequestException(objectType, error);
23+
public void validationError(String objectType, com.xero.models.accounting.Error error, Exception e) {
24+
throw new XeroBadRequestException(objectType, error, e);
2525
}
2626

2727
// ASSETS Validation Errors (400)
28-
public void validationError(String objectType, com.xero.models.assets.Error error) {
29-
throw new XeroBadRequestException(objectType, error);
28+
public void validationError(String objectType, com.xero.models.assets.Error error, Exception e) {
29+
throw new XeroBadRequestException(objectType, error, e);
3030
}
3131

3232
// BANKFEED Statements Validation Errors (400)
33-
public void validationError(String objectType, Statements error) {
34-
throw new XeroBadRequestException(objectType, error);
33+
public void validationError(String objectType, Statements error, Exception e) {
34+
throw new XeroBadRequestException(objectType, error, e);
3535
}
3636

3737
// BANKFEED Connections Validation Errors (400)
38-
public void validationError(String objectType, FeedConnections error) {
39-
throw new XeroBadRequestException(objectType, error);
38+
public void validationError(String objectType, FeedConnections error, Exception e) {
39+
throw new XeroBadRequestException(objectType, error, e);
4040
}
4141

4242
// PAYROLL UK Validation Errors
43-
public void validationError(Integer statusCode, String objectType, com.xero.models.payrolluk.Problem error) {
43+
public void validationError(Integer statusCode, String objectType, com.xero.models.payrolluk.Problem error, Exception e) {
4444
if (statusCode == 400 ) {
45-
throw new XeroBadRequestException(objectType, error);
45+
throw new XeroBadRequestException(objectType, error, e);
4646
} else if(statusCode == 405) {
47-
throw new XeroMethodNotAllowedException(objectType, error);
47+
throw new XeroMethodNotAllowedException(objectType, error, e);
4848
}
4949
}
5050

5151
// PAYROLL NZ Validation Errors
52-
public void validationError(Integer statusCode, String objectType, com.xero.models.payrollnz.Problem error) {
52+
public void validationError(Integer statusCode, String objectType, com.xero.models.payrollnz.Problem error, Exception e) {
5353
if (statusCode == 400 ) {
54-
throw new XeroBadRequestException(objectType, error);
54+
throw new XeroBadRequestException(objectType, error, e);
5555
} else if(statusCode == 405) {
56-
throw new XeroMethodNotAllowedException(objectType, error);
56+
throw new XeroMethodNotAllowedException(objectType, error, e);
5757
} else if(statusCode == 409) {
58-
throw new XeroConflictException(objectType, error);
58+
throw new XeroConflictException(objectType, error, e);
5959
}
6060
}
6161

6262
// PAYROLL AU Employees Validation Errors (400)
63-
public void validationError(String objectType, com.xero.models.payrollau.Employees employees) {
64-
throw new XeroBadRequestException(objectType, employees);
63+
public void validationError(String objectType, com.xero.models.payrollau.Employees employees, Exception e) {
64+
throw new XeroBadRequestException(objectType, employees, e);
6565
}
6666

67-
public void validationError(String objectType, com.xero.models.payrollau.LeaveApplications leaveApplications) {
68-
throw new XeroBadRequestException(objectType, leaveApplications);
67+
public void validationError(String objectType, com.xero.models.payrollau.LeaveApplications leaveApplications, Exception e) {
68+
throw new XeroBadRequestException(objectType, leaveApplications, e);
6969
}
7070

71-
public void validationError(String objectType, com.xero.models.payrollau.PayItems payItems) {
72-
throw new XeroBadRequestException(objectType, payItems);
71+
public void validationError(String objectType, com.xero.models.payrollau.PayItems payItems, Exception e) {
72+
throw new XeroBadRequestException(objectType, payItems, e);
7373
}
7474

75-
public void validationError(String objectType, com.xero.models.payrollau.PayRuns payRuns) {
76-
throw new XeroBadRequestException(objectType, payRuns);
75+
public void validationError(String objectType, com.xero.models.payrollau.PayRuns payRuns, Exception e) {
76+
throw new XeroBadRequestException(objectType, payRuns, e);
7777
}
7878

79-
public void validationError(String objectType, com.xero.models.payrollau.PayrollCalendars payrollCalendars) {
80-
throw new XeroBadRequestException(objectType, payrollCalendars);
79+
public void validationError(String objectType, com.xero.models.payrollau.PayrollCalendars payrollCalendars, Exception e) {
80+
throw new XeroBadRequestException(objectType, payrollCalendars, e);
8181
}
8282

83-
public void validationError(String objectType, com.xero.models.payrollau.SuperFunds superFunds) {
84-
throw new XeroBadRequestException(objectType, superFunds);
83+
public void validationError(String objectType, com.xero.models.payrollau.SuperFunds superFunds, Exception e) {
84+
throw new XeroBadRequestException(objectType, superFunds, e);
8585
}
8686

87-
public void validationError(String objectType, com.xero.models.payrollau.Timesheets timesheets) {
88-
throw new XeroBadRequestException(objectType, timesheets);
87+
public void validationError(String objectType, com.xero.models.payrollau.Timesheets timesheets, Exception e) {
88+
throw new XeroBadRequestException(objectType, timesheets, e);
8989
}
9090

91-
public void validationError(String objectType, String msg) {
92-
throw new XeroBadRequestException(400, msg);
91+
public void validationError(String objectType, String msg, Exception e) {
92+
throw new XeroBadRequestException(400, msg, e);
9393
}
9494

9595
// REFACTOR GENERIC ERROR HANDLER
@@ -98,34 +98,34 @@ public void execute(HttpResponseException e) {
9898

9999
if (statusCode == 400) {
100100
String message = e.getMessage();
101-
throw new XeroBadRequestException(statusCode,message);
102-
101+
throw new XeroBadRequestException(statusCode, message, e);
102+
103103
} else if (statusCode == 401) {
104104
String message = "Unauthorized - check your scopes and confirm access to this resource";
105-
throw new XeroUnauthorizedException(statusCode, message);
105+
throw new XeroUnauthorizedException(statusCode, message, e);
106106

107107
} else if (statusCode == 403) {
108108
String message = "Forbidden - authentication unsuccessful";
109-
throw new XeroForbiddenException(statusCode, message);
109+
throw new XeroForbiddenException(statusCode, message, e);
110110

111111
} else if (statusCode == 404) {
112112
String message = "The resource you're looking for cannot be found";
113-
throw new XeroNotFoundException(statusCode, message);
113+
throw new XeroNotFoundException(statusCode, message, e);
114114

115115
} else if (statusCode == 429) {
116116
String message = "You've exceeded the per " + e.getHeaders().get("x-rate-limit-problem") + " rate limit";
117-
throw new XeroRateLimitException(statusCode, message);
117+
throw new XeroRateLimitException(statusCode, message, e);
118118

119119
} else if (statusCode == 500) {
120120
String message = "An error occurred in Xero. Check the API Status page http://status.developer.xero.com for current service status.";
121-
throw new XeroServerErrorException(statusCode, message);
121+
throw new XeroServerErrorException(statusCode, message, e);
122122

123123
} else if (statusCode > 500) {
124124
String message = "Internal Server Error";
125-
throw new XeroServerErrorException(statusCode, message);
125+
throw new XeroServerErrorException(statusCode, message, e);
126126

127127
} else {
128-
throw new XeroApiException(statusCode, e.getStatusMessage());
128+
throw new XeroApiException(statusCode, e.getStatusMessage(), e);
129129
}
130130
}
131131

@@ -139,27 +139,27 @@ public void execute(HttpResponseException e, ApiClient apiClient)
139139
TypeReference<Error> errorTypeRef = new TypeReference<Error>() {
140140
};
141141
error = apiClient.getObjectMapper().readValue(e.getContent(), errorTypeRef);
142-
throw new XeroApiException(statusCode, e.getStatusMessage(), error);
142+
throw new XeroApiException(statusCode, e.getStatusMessage(), error, e);
143143
} else if (statusCode == 404) {
144144
error = new Error();
145145
Element elementsItem = new Element();
146146
ValidationError ve = new ValidationError();
147147
ve.setMessage("The resource you're looking for cannot be found");
148148
elementsItem.addValidationErrorsItem(ve);
149149
error.addElementsItem(elementsItem);
150-
throw new XeroApiException(statusCode, error);
150+
throw new XeroApiException(statusCode, error, e);
151151
} else if (statusCode == 429) {
152152
error = new Error();
153153
Element elementsItem = new Element();
154154
ValidationError ve = new ValidationError();
155155
ve.setMessage("You've exceeded the per " + e.getHeaders().get("x-rate-limit-problem") + " rate limit");
156156
elementsItem.addValidationErrorsItem(ve);
157157
error.addElementsItem(elementsItem);
158-
throw new XeroApiException(statusCode, error);
158+
throw new XeroApiException(statusCode, error, e);
159159
} else if (statusCode == 401) {
160-
throw new XeroApiException(401, "Unauthorized - check your scopes and confirm access to this resource" );
160+
throw new XeroApiException(statusCode, "Unauthorized - check your scopes and confirm access to this resource", e);
161161
} else if (statusCode != 400) {
162-
throw new XeroApiException(statusCode, e.getStatusMessage());
162+
throw new XeroApiException(statusCode, e.getStatusMessage(), e);
163163
} else {
164164
throw e;
165165
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class XeroAuthenticationException extends XeroException {
77
private static final long serialVersionUID = -6292824871010327632L;
88

9-
public XeroAuthenticationException(final String message) {
10-
super(message);
9+
public XeroAuthenticationException(final String message, final Exception e) {
10+
super(message, e);
1111
}
1212
}

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xero.api;
22

3+
import com.xero.models.payrollau.Timesheets;
34
import java.util.ArrayList;
45
import java.util.List;
56
import java.util.Objects;
@@ -34,79 +35,91 @@ public class XeroBadRequestException extends XeroException {
3435
private List<com.xero.models.payrollau.Timesheet> timesheetItems = new ArrayList<com.xero.models.payrollau.Timesheet>();
3536
private List<com.xero.models.payrollau.PayRun> payRunItems = new ArrayList<com.xero.models.payrollau.PayRun>();
3637

37-
public XeroBadRequestException(String objectType, com.xero.models.accounting.Error error) {
38+
public XeroBadRequestException(String objectType, com.xero.models.accounting.Error error, Exception e) {
39+
super(e);
3840
this.statusCode = 400;
3941
this.type(objectType);
4042
this.elements(error.getElements());
4143
}
4244

43-
public XeroBadRequestException(String objectType, com.xero.models.assets.Error error) {
45+
public XeroBadRequestException(String objectType, com.xero.models.assets.Error error, Exception e) {
46+
super(e);
4447
this.statusCode = 400;
4548
this.type = objectType;
4649
this.fieldValidationErrorsElements = error.getFieldValidationErrors();
4750
}
4851

49-
public XeroBadRequestException(String objectType, Statements error) {
52+
public XeroBadRequestException(String objectType, Statements error, Exception e) {
53+
super(e);
5054
this.statusCode = 400;
5155
this.type = objectType;
5256
this.statementItems = error.getItems();
5357
}
5458

55-
public XeroBadRequestException(String objectType, FeedConnections error) {
59+
public XeroBadRequestException(String objectType, FeedConnections error, Exception e) {
60+
super(e);
5661
this.statusCode = 400;
5762
this.type = objectType;
5863
this.feedConnectionItems = error.getItems();
5964
}
6065

61-
public XeroBadRequestException(String objectType, com.xero.models.payrolluk.Problem problem) {
66+
public XeroBadRequestException(String objectType, com.xero.models.payrolluk.Problem problem, Exception e) {
67+
super(e);
6268
this.statusCode = 400;
6369
this.type = objectType;
6470
this.payrollUkProblem = problem;
6571
}
6672

67-
public XeroBadRequestException(String objectType, com.xero.models.payrollnz.Problem problem) {
73+
public XeroBadRequestException(String objectType, com.xero.models.payrollnz.Problem problem, Exception e) {
74+
super(e);
6875
this.statusCode = 400;
6976
this.type = objectType;
7077
this.payrollNzProblem = problem;
7178
}
72-
73-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.Employees employees) {
79+
80+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.Employees employees, Exception e) {
81+
super(e);
7482
this.statusCode = 400;
7583
this.type = objectType;
7684
this.employeeItems = employees.getEmployees();
7785
}
7886

79-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.LeaveApplications leaveApplications) {
87+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.LeaveApplications leaveApplications, Exception e) {
88+
super(e);
8089
this.statusCode = 400;
8190
this.type = objectType;
8291
this.leaveApplicationItems = leaveApplications.getLeaveApplications();
8392
}
8493

85-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.PayItems payItems) {
94+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.PayItems payItems, Exception e) {
95+
super(e);
8696
this.statusCode = 400;
8797
this.type = objectType;
8898
//this.payItemItems = payItems.g
8999
}
90100

91-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.PayrollCalendars payrollCalendars) {
101+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.PayrollCalendars payrollCalendars, Exception e) {
102+
super(e);
92103
this.statusCode = 400;
93104
this.type = objectType;
94105
this.payrollCalendarItems = payrollCalendars.getPayrollCalendars();
95106
}
96107

97-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.SuperFunds superFunds) {
108+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.SuperFunds superFunds, Exception e) {
98109
this.statusCode = 400;
99110
this.type = objectType;
100111
this.superFundItems = superFunds.getSuperFunds();
101112
}
102113

103-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.Timesheets timesheets) {
114+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.Timesheets timesheets, Exception e) {
115+
super(e);
104116
this.statusCode = 400;
105117
this.type = objectType;
106118
this.timesheetItems = timesheets.getTimesheets();
107119
}
108120

109-
public XeroBadRequestException(String objectType, com.xero.models.payrollau.PayRuns payRuns) {
121+
public XeroBadRequestException(String objectType, com.xero.models.payrollau.PayRuns payRuns, Exception e) {
122+
super(e);
110123
this.statusCode = 400;
111124
this.type = objectType;
112125
this.payRunItems = payRuns.getPayRuns();
@@ -116,7 +129,13 @@ public XeroBadRequestException(Integer statusCode, String message) {
116129
this.statusCode = statusCode;
117130
this.message = message;
118131
}
119-
132+
133+
public XeroBadRequestException(Integer statusCode, String message, Exception e) {
134+
super(message, e);
135+
this.statusCode = statusCode;
136+
this.message = message;
137+
}
138+
120139
public XeroBadRequestException statusCode(Integer statusCode) {
121140
this.statusCode = statusCode;
122141
return this;
@@ -304,7 +323,7 @@ public XeroBadRequestException payrollNzProblem(com.xero.models.payrollnz.Proble
304323
this.payrollNzProblem = problem;
305324
return this;
306325
}
307-
326+
308327
/**
309328
* Exception type
310329
* @return com.xero.models.payrollnz.Problem
@@ -313,11 +332,11 @@ public XeroBadRequestException payrollNzProblem(com.xero.models.payrollnz.Proble
313332
public com.xero.models.payrollnz.Problem getPayrollNzProblem() {
314333
return payrollNzProblem;
315334
}
316-
335+
317336
public void setPayrollNzProblem(com.xero.models.payrollnz.Problem problem) {
318337
this.payrollNzProblem = problem;
319338
}
320-
339+
321340

322341
// Payroll AU Employees
323342
public XeroBadRequestException employeeItemsItems(List<com.xero.models.payrollau.Employee> employeeItems) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public XeroConflictException(String objectType, com.xero.models.payrolluk.Proble
5858
this.payrollUkProblem = problem;
5959
}
6060

61-
public XeroConflictException(String objectType, com.xero.models.payrollnz.Problem problem) {
61+
public XeroConflictException(String objectType, com.xero.models.payrollnz.Problem problem, Exception e) {
62+
super(e);
6263
this.statusCode = 409;
6364
this.type = objectType;
6465
this.payrollNzProblem = problem;

0 commit comments

Comments
 (0)