Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
sdk.iml

*.class
.settings/
.classpath
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>

Expand Down
13 changes: 3 additions & 10 deletions src/main/java/me/figo/FigoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import me.figo.internal.FigoSocketFactory;
import me.figo.internal.FigoTrustManager;
import me.figo.internal.GsonAdapter;
import me.figo.models.ErrorResponse;

/**
*
Expand Down Expand Up @@ -187,19 +188,11 @@ protected <T> T processResponse(HttpURLConnection connection, Type typeOfT) thro
if (code >= 200 && code < 300) {
return handleResponse(connection.getInputStream(), typeOfT);
} else {
FigoException.ErrorResponse errorResponse = handleResponse(connection.getErrorStream(), FigoException.ErrorResponse.class);
logError(errorResponse, connection);
throw new FigoException(errorResponse);
ErrorResponse errorResponse = handleResponse(connection.getErrorStream(), ErrorResponse.class);
throw new FigoApiException(errorResponse);
}
}

private void logError(FigoException.ErrorResponse errorResponse, HttpURLConnection connection) {
String errorString = errorResponse.getError().toString();
if (connection != null)
errorString += " " + connection.getRequestMethod() + " " + connection.getURL().toString();
logger.log(Level.SEVERE, errorString);
}

/**
* Handle the response of a request by decoding its JSON payload
*
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/me/figo/FigoApiException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.figo;

import me.figo.models.ErrorObject;
import me.figo.models.ErrorResponse;

public class FigoApiException extends FigoException {

private static final long serialVersionUID = -1855328588622789903L;

private ErrorObject error;

public FigoApiException(ErrorResponse response) {
super(response.getError().toString(), response.getError().getDescription());
this.error = response.getError();
}

public ErrorObject getError() {
return error;
}

}
15 changes: 5 additions & 10 deletions src/main/java/me/figo/FigoConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,16 @@

package me.figo;

import com.google.common.io.BaseEncoding;
import me.figo.internal.*;
import me.figo.models.BusinessProcess;
import me.figo.models.ProcessToken;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import com.google.common.io.BaseEncoding;

import me.figo.internal.CreateUserRequest;
import me.figo.internal.CreateUserResponse;
import me.figo.internal.CredentialLoginRequest;
import me.figo.internal.TokenRequest;
import me.figo.internal.TokenResponse;
import me.figo.models.BusinessProcess;
import me.figo.models.ProcessToken;


/**
* 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.
Expand Down
90 changes: 3 additions & 87 deletions src/main/java/me/figo/FigoException.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

package me.figo;

import com.google.gson.annotations.Expose;

/***
* Base Class for all figo Exceptions. It extends the normal Java exceptions with an error_code field, which carries the computer readable error reason.
*
Expand All @@ -37,21 +35,10 @@ public class FigoException extends Exception {
private final String error_description;

public FigoException(String error_message, String error_description) {
super(error_message);

this.error_message = error_message;
this.error_description = error_description;
}

public FigoException(String error_message, String error_description, Throwable exc) {
super(error_message, exc);

this.error_message = error_message;
this.error_description = error_description;
}
super(error_message);

public FigoException(ErrorResponse response) {
this(response.getError().getMessage(), response.getError().getDescription());
this.error_message = error_message;
this.error_description = error_description;
}

public String getErrorMessage() {
Expand All @@ -62,75 +49,4 @@ public String getErrorDescription() {
return error_description;
}

public static class ErrorResponse {

@Expose
private ErrorObject error;

public ErrorResponse() {
}

public ErrorObject getError() {
return error;
}
}

public static class ErrorObject {

@Expose
private String code;

@Expose
private String name;

@Expose
private String message;

@Expose
private String description;

@Expose
private String group;

public ErrorObject() {
}

public String getMessage() {
return message;
}

public String getDescription() {
return description;
}

public String getCode() {
return code;
}

public String getName() {
return name;
}

public String getGroup() {
return group;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ErrorObject [");
if (code != null)
builder.append("code=").append(code).append(", ");
if (name != null)
builder.append("name=").append(name).append(", ");
if (message != null)
builder.append("message=").append(message).append(", ");
if (description != null)
builder.append("description=").append(description).append(", ");
if (group != null)
builder.append("group=").append(group);
builder.append("]");
return builder.toString();
}
}
}
75 changes: 75 additions & 0 deletions src/main/java/me/figo/models/ErrorObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package me.figo.models;

import com.google.gson.annotations.Expose;

import java.util.List;
import java.util.Map;

public class ErrorObject {

@Expose
private String code;

@Expose
private String name;

@Expose
private String message;

@Expose
private String description;

@Expose
private String group;

@Expose
private Map<String, List<String>> data;

public ErrorObject() {
}

public String getMessage() {
return message;
}

public String getDescription() {
return description;
}

public String getCode() {
return code;
}

public String getName() {
return name;
}

public String getGroup() {
return group;
}

public Map<String, List<String>> getData() {
return data;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ErrorObject [");
if (code != null)
builder.append("code=").append(code).append(", ");
if (name != null)
builder.append("name=").append(name).append(", ");
if (message != null)
builder.append("message=").append(message).append(", ");
if (description != null)
builder.append("description=").append(description).append(", ");
if (data != null)
builder.append("data=").append(data.toString()).append(", ");
if (group != null)
builder.append("group=").append(group);
builder.append("]");
return builder.toString();
}

}
17 changes: 17 additions & 0 deletions src/main/java/me/figo/models/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.figo.models;

import com.google.gson.annotations.Expose;

public class ErrorResponse {

@Expose
private ErrorObject error;

public ErrorResponse() {
}

public ErrorObject getError() {
return error;
}

}
29 changes: 29 additions & 0 deletions src/test/java/me/figo/FigoApiExceptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.figo;

import com.google.gson.Gson;
import me.figo.internal.GsonAdapter;
import me.figo.models.ErrorResponse;
import org.junit.Assert;
import org.junit.Test;

public class FigoApiExceptionTest {

@Test
public void ensureNonNullMessage() {
Gson g = GsonAdapter.createGson();

String errorResponse = "{\n" +
" \"error\": {\n" +
" \"code\": 1000,\n" +
" \"description\": \"Request body doesn't match input schema.\"\n" +
" }\n" +
"}";

ErrorResponse resp = g.fromJson(errorResponse, ErrorResponse.class);
FigoApiException ex = new FigoApiException(resp);

Assert.assertNotNull(ex.getMessage());
Assert.assertEquals(ex.getMessage(), ex.getError().toString());
}

}
2 changes: 1 addition & 1 deletion src/test/java/me/figo/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void testGetErrorMessage() throws IOException {
fail(acc.getName());
}
catch(FigoException e) {
assertEquals(null, e.getErrorMessage());
assertEquals("ErrorObject [code=1002, description=Not Found, data={}, group=client]", e.getErrorMessage());
assertEquals("Not Found", e.getErrorDescription());
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/me/figo/models/ErrorObjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.figo.models;

import com.google.gson.Gson;
import me.figo.internal.GsonAdapter;
import org.junit.Assert;
import org.junit.Test;

public class ErrorObjectTest {

@Test
public void testGson() {
Gson g = GsonAdapter.createGson();

String errorResponse = "{\n" +
" \"error\": {\n" +
" \"code\": 1000,\n" +
" \"data\": {\"bank_code\": [\"Not a valid string.\"],\n" +
" \"credentials\": [\"Credentials must contain at least 2 strings.\"]},\n" +
" \"description\": \"Request body doesn't match input schema.\",\n" +
" \"group\": \"client\"\n" +
" }\n" +
"}";

ErrorResponse resp = g.fromJson(errorResponse, ErrorResponse.class);

ErrorObject obj = resp.getError();

Assert.assertNotNull(obj.getData());
Assert.assertTrue(obj.getData().containsKey("bank_code"));
Assert.assertEquals(obj.getData().get("bank_code").get(0), "Not a valid string.");
}

}