Skip to content

Commit c14bada

Browse files
committed
Add api error object to exception
* Move ErrorObject and ErrorResponse to public model * Upgrade guava
1 parent 76a1f62 commit c14bada

File tree

11 files changed

+196
-105
lines changed

11 files changed

+196
-105
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.idea
2+
sdk.iml
3+
14
*.class
25
.settings/
36
.classpath

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@
7474
<scope>test</scope>
7575
</dependency>
7676
<dependency>
77-
<groupId>com.google.guava</groupId>
78-
<artifactId>guava</artifactId>
79-
<version>14.0.1</version>
77+
<groupId>com.google.guava</groupId>
78+
<artifactId>guava</artifactId>
79+
<version>18.0</version>
8080
</dependency>
8181
</dependencies>
8282

src/main/java/me/figo/FigoApi.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import me.figo.internal.FigoSocketFactory;
4747
import me.figo.internal.FigoTrustManager;
4848
import me.figo.internal.GsonAdapter;
49+
import me.figo.models.ErrorResponse;
4950

5051
/**
5152
*
@@ -187,19 +188,19 @@ protected <T> T processResponse(HttpURLConnection connection, Type typeOfT) thro
187188
if (code >= 200 && code < 300) {
188189
return handleResponse(connection.getInputStream(), typeOfT);
189190
} else {
190-
FigoException.ErrorResponse errorResponse = handleResponse(connection.getErrorStream(), FigoException.ErrorResponse.class);
191+
ErrorResponse errorResponse = handleResponse(connection.getErrorStream(), ErrorResponse.class);
191192
logError(errorResponse, connection);
192-
throw new FigoException(errorResponse);
193+
throw new FigoApiException(errorResponse);
193194
}
194195
}
195196

196-
private void logError(FigoException.ErrorResponse errorResponse, HttpURLConnection connection) {
197+
private void logError(ErrorResponse errorResponse, HttpURLConnection connection) {
197198
String errorString = errorResponse.getError().toString();
198199
if (connection != null)
199200
errorString += " " + connection.getRequestMethod() + " " + connection.getURL().toString();
200201
logger.log(Level.SEVERE, errorString);
201202
}
202-
203+
203204
/**
204205
* Handle the response of a request by decoding its JSON payload
205206
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package me.figo;
2+
3+
import me.figo.models.ErrorObject;
4+
import me.figo.models.ErrorResponse;
5+
6+
public class FigoApiException extends FigoException {
7+
8+
private static final long serialVersionUID = -1855328588622789903L;
9+
10+
private ErrorObject error;
11+
12+
public FigoApiException(ErrorResponse response) {
13+
super(response.getError().getMessage() != null ? response.getError().getMessage() : response.getError().getDescription(),
14+
response.getError().getDescription());
15+
this.error = response.getError();
16+
}
17+
18+
public ErrorObject getError() {
19+
return error;
20+
}
21+
22+
}

src/main/java/me/figo/FigoConnection.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,16 @@
2222

2323
package me.figo;
2424

25+
import com.google.common.io.BaseEncoding;
26+
import me.figo.internal.*;
27+
import me.figo.models.BusinessProcess;
28+
import me.figo.models.ProcessToken;
29+
2530
import java.io.IOException;
2631
import java.io.UnsupportedEncodingException;
2732
import java.net.URLEncoder;
2833
import java.nio.charset.Charset;
2934

30-
import com.google.common.io.BaseEncoding;
31-
32-
import me.figo.internal.CreateUserRequest;
33-
import me.figo.internal.CreateUserResponse;
34-
import me.figo.internal.CredentialLoginRequest;
35-
import me.figo.internal.TokenRequest;
36-
import me.figo.internal.TokenResponse;
37-
import me.figo.models.BusinessProcess;
38-
import me.figo.models.ProcessToken;
39-
4035

4136
/**
4237
* Representing a not user-bound connection to the figo connect API. Its main purpose is to let user login via the OAuth2 API and/or create business processes.

src/main/java/me/figo/FigoException.java

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
package me.figo;
2424

25-
import com.google.gson.annotations.Expose;
26-
2725
/***
2826
* Base Class for all figo Exceptions. It extends the normal Java exceptions with an error_code field, which carries the computer readable error reason.
2927
*
@@ -37,21 +35,10 @@ public class FigoException extends Exception {
3735
private final String error_description;
3836

3937
public FigoException(String error_message, String error_description) {
40-
super(error_message);
41-
42-
this.error_message = error_message;
43-
this.error_description = error_description;
44-
}
45-
46-
public FigoException(String error_message, String error_description, Throwable exc) {
47-
super(error_message, exc);
48-
49-
this.error_message = error_message;
50-
this.error_description = error_description;
51-
}
38+
super(error_message);
5239

53-
public FigoException(ErrorResponse response) {
54-
this(response.getError().getMessage(), response.getError().getDescription());
40+
this.error_message = error_message;
41+
this.error_description = error_description;
5542
}
5643

5744
public String getErrorMessage() {
@@ -62,75 +49,4 @@ public String getErrorDescription() {
6249
return error_description;
6350
}
6451

65-
public static class ErrorResponse {
66-
67-
@Expose
68-
private ErrorObject error;
69-
70-
public ErrorResponse() {
71-
}
72-
73-
public ErrorObject getError() {
74-
return error;
75-
}
76-
}
77-
78-
public static class ErrorObject {
79-
80-
@Expose
81-
private String code;
82-
83-
@Expose
84-
private String name;
85-
86-
@Expose
87-
private String message;
88-
89-
@Expose
90-
private String description;
91-
92-
@Expose
93-
private String group;
94-
95-
public ErrorObject() {
96-
}
97-
98-
public String getMessage() {
99-
return message;
100-
}
101-
102-
public String getDescription() {
103-
return description;
104-
}
105-
106-
public String getCode() {
107-
return code;
108-
}
109-
110-
public String getName() {
111-
return name;
112-
}
113-
114-
public String getGroup() {
115-
return group;
116-
}
117-
118-
@Override
119-
public String toString() {
120-
StringBuilder builder = new StringBuilder();
121-
builder.append("ErrorObject [");
122-
if (code != null)
123-
builder.append("code=").append(code).append(", ");
124-
if (name != null)
125-
builder.append("name=").append(name).append(", ");
126-
if (message != null)
127-
builder.append("message=").append(message).append(", ");
128-
if (description != null)
129-
builder.append("description=").append(description).append(", ");
130-
if (group != null)
131-
builder.append("group=").append(group);
132-
builder.append("]");
133-
return builder.toString();
134-
}
135-
}
13652
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package me.figo.models;
2+
3+
import com.google.gson.annotations.Expose;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class ErrorObject {
9+
10+
@Expose
11+
private String code;
12+
13+
@Expose
14+
private String name;
15+
16+
@Expose
17+
private String message;
18+
19+
@Expose
20+
private String description;
21+
22+
@Expose
23+
private String group;
24+
25+
@Expose
26+
private Map<String, List<String>> data;
27+
28+
public ErrorObject() {
29+
}
30+
31+
public String getMessage() {
32+
return message;
33+
}
34+
35+
public String getDescription() {
36+
return description;
37+
}
38+
39+
public String getCode() {
40+
return code;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public String getGroup() {
48+
return group;
49+
}
50+
51+
public Map<String, List<String>> getData() {
52+
return data;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
StringBuilder builder = new StringBuilder();
58+
builder.append("ErrorObject [");
59+
if (code != null)
60+
builder.append("code=").append(code).append(", ");
61+
if (name != null)
62+
builder.append("name=").append(name).append(", ");
63+
if (message != null)
64+
builder.append("message=").append(message).append(", ");
65+
if (description != null)
66+
builder.append("description=").append(description).append(", ");
67+
if (data != null)
68+
builder.append("data=").append(data.toString());
69+
if (group != null)
70+
builder.append("group=").append(group);
71+
builder.append("]");
72+
return builder.toString();
73+
}
74+
75+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.figo.models;
2+
3+
import com.google.gson.annotations.Expose;
4+
5+
public class ErrorResponse {
6+
7+
@Expose
8+
private ErrorObject error;
9+
10+
public ErrorResponse() {
11+
}
12+
13+
public ErrorObject getError() {
14+
return error;
15+
}
16+
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package me.figo;
2+
3+
import com.google.gson.Gson;
4+
import me.figo.internal.GsonAdapter;
5+
import me.figo.models.ErrorResponse;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class FigoApiExceptionTest {
10+
11+
@Test
12+
public void ensureNonNullMessage() {
13+
Gson g = GsonAdapter.createGson();
14+
15+
String errorResponse = "{\n" +
16+
" \"error\": {\n" +
17+
" \"code\": 1000,\n" +
18+
" \"description\": \"Request body doesn't match input schema.\"\n" +
19+
" }\n" +
20+
"}";
21+
22+
ErrorResponse resp = g.fromJson(errorResponse, ErrorResponse.class);
23+
FigoException ex = new FigoApiException(resp);
24+
25+
Assert.assertNotNull(ex.getMessage());
26+
Assert.assertEquals(ex.getMessage(), ex.getErrorDescription());
27+
}
28+
29+
}

src/test/java/me/figo/SessionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void testGetErrorMessage() throws IOException {
153153
fail(acc.getName());
154154
}
155155
catch(FigoException e) {
156-
assertEquals(null, e.getErrorMessage());
156+
assertEquals("Not Found", e.getErrorMessage());
157157
assertEquals("Not Found", e.getErrorDescription());
158158
}
159159
}

0 commit comments

Comments
 (0)