Skip to content

Commit 41b5ba8

Browse files
committed
Merge PR #48
If config.json missing - throw a runtime error and catch it.
1 parent 611cebb commit 41b5ba8

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.InputStreamReader;
7+
import static java.lang.String.format;
78

89
import org.json.simple.JSONObject;
910
import org.json.simple.parser.JSONParser;
@@ -167,8 +168,11 @@ public void setAuthCallBackUrl(String authCallbackUrl) {
167168
AUTH_CALLBACK_URL = authCallbackUrl;
168169
}
169170

170-
public void load() {
171+
private void load() {
171172
InputStream inputStream = JsonConfig.class.getResourceAsStream("/" + configFile);
173+
if (inputStream == null) {
174+
throw new XeroClientException(format("Config file '%s' could not be opened. Missing file?", configFile));
175+
}
172176
InputStreamReader reader = new InputStreamReader(inputStream);
173177

174178
JSONParser parser = new JSONParser();
@@ -177,11 +181,11 @@ public void load() {
177181
try {
178182
obj = parser.parse(reader);
179183
} catch (FileNotFoundException e) {
180-
e.printStackTrace();
184+
throw new XeroClientException(format("Config file '%s' not found", configFile), e);
181185
} catch (IOException e) {
182-
e.printStackTrace();
186+
throw new XeroClientException(format("IO error reading config file '%s' not found", configFile), e);
183187
} catch (ParseException e) {
184-
e.printStackTrace();
188+
throw new XeroClientException(format("Parse error reading config file '%s' not found", configFile), e);
185189
}
186190
JSONObject jsonObject = (JSONObject) obj;
187191

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.xero.api;
2+
3+
/**
4+
* Exception thrown by Xero Java API if there is an error which is not related
5+
* to remote API calls (like for example no config).
6+
*
7+
* @author GideonLeGrange <gideon@legrange.me>
8+
*/
9+
public class XeroClientException extends RuntimeException {
10+
11+
/**
12+
* Create new exception.
13+
*
14+
* @param message Error message
15+
*/
16+
public XeroClientException(String message) {
17+
super(message);
18+
}
19+
20+
/**
21+
* Create new exception.
22+
*
23+
* @param message Error message
24+
* @param cause Original exception causing this error.
25+
*/
26+
public XeroClientException(String message, Throwable cause) {
27+
super(message, cause);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)