Skip to content

Commit

Permalink
refactored error handling and error object, improved error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
danielseligmann committed Aug 8, 2017
1 parent 8f6668c commit e9f91fa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/main/java/me/figo/FigoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
Expand All @@ -53,6 +55,7 @@ public class FigoApi {

protected static final String API_FIGO_LIVE = "https://api.figo.me";
protected static final String API_FIGO_STAGE = "https://staging.figo.me";
private Logger logger = Logger.getLogger(this.getClass().getName());
private final String apiEndpoint;
private final String authorization;
private int timeout;
Expand Down Expand Up @@ -183,17 +186,16 @@ protected <T> T processResponse(HttpURLConnection connection, Type typeOfT) thro
int code = connection.getResponseCode();
if (code >= 200 && code < 300) {
return handleResponse(connection.getInputStream(), typeOfT);
} else if (code == 400) {
throw new FigoException((FigoException.ErrorResponse) handleResponse(connection.getErrorStream(), FigoException.ErrorResponse.class));
} else if (code == 401) {
throw new FigoException("access_denied", "Access Denied");
} else if (code == 404) {
throw new FigoException("Entry not found.", null);
} else {
// return decode(connection.getErrorStream(), resultType);
throw new FigoException("internal_server_error", "We are very sorry, but something went wrong");
FigoException.ErrorResponse errorResponse = (FigoException.ErrorResponse) handleResponse(connection.getErrorStream(), FigoException.ErrorResponse.class);
logError(errorResponse);
throw new FigoException(errorResponse);
}
}

private void logError(FigoException.ErrorResponse errorResponse) {
logger.log(Level.SEVERE,errorResponse.getError().toString());
}

/**
* Handle the response of a request by decoding its JSON payload
Expand Down
51 changes: 45 additions & 6 deletions src/main/java/me/figo/FigoException.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,21 @@ public ErrorObject getError() {
}

public static class ErrorObject {

@Expose
private String message;

@Expose
private String description;

@Expose
private String code;

@Expose
private String name;

@Expose
private String message;

@Expose
private String description;

@Expose
private String group;

public ErrorObject() {
}
Expand All @@ -93,5 +102,35 @@ public String getMessage() {
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();
}
}
}
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 @@ -142,7 +142,7 @@ public void testGetErrorMessage() throws IOException {
fail(acc.getName());
}
catch(FigoException e) {
assertEquals("Entry not found.", e.getErrorMessage());
assertEquals("Not Found", e.getErrorMessage());
assertEquals(null, e.getErrorDescription());
}
}
Expand Down

0 comments on commit e9f91fa

Please sign in to comment.