Skip to content

Commit

Permalink
Merge pull request #9 from Invoiced/error_handling
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
jared-invoiced authored Dec 4, 2017
2 parents 17bea4e + 57bb52d commit c888e55
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 64 deletions.
81 changes: 38 additions & 43 deletions src/main/java/com/invoiced/entity/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.invoiced.exception.ApiException;
import com.invoiced.exception.AuthException;
import com.invoiced.exception.ConnException;
import com.invoiced.exception.InvalidRequestException;
import com.invoiced.exception.InvoicedException;
import com.invoiced.exception.RateLimitException;
import com.invoiced.util.ListResponse;
import com.invoiced.util.Util;
import com.mashape.unirest.http.HttpResponse;
Expand All @@ -23,8 +26,7 @@ public class Connection {
private boolean testMode;
private boolean autoRefresh;

protected String post(String url, HashMap<String, Object> queryParms, String jsonBody)
throws ApiException, AuthException, ConnException {
protected String post(String url, HashMap<String, Object> queryParms, String jsonBody) throws InvoicedException {

String responseString = "";
int responseCode = -1;
Expand All @@ -35,19 +37,17 @@ protected String post(String url, HashMap<String, Object> queryParms, String jso

try {
HttpResponse<String> response = Unirest.post(url).basicAuth(this.apiKey, "")
.header("accept", Connection.Accept).header("Content-Type", "application/json")
.queryString(queryParms).body(jsonBody).asString();
.header("accept", Connection.Accept).header("Content-Type", "application/json")
.queryString(queryParms).body(jsonBody).asString();
responseString = response.getBody().toString();
responseCode = response.getStatus();

} catch (Throwable c) {
throw new ConnException(c);
}

if (responseCode == 401) {
throw new AuthException(responseString);
} else if (responseCode == 400 || responseCode == 403 || responseCode == 404) {
throw new ApiException(responseString);
if (responseCode >= 400) {
throw this.handleApiError(responseCode, responseString);
}

return responseString;
Expand All @@ -61,7 +61,7 @@ public static void closeAll() {
}
}

protected String patch(String url, String jsonBody) throws ApiException, AuthException, ConnException {
protected String patch(String url, String jsonBody) throws InvoicedException {

String responseString = "";
int responseCode = -1;
Expand All @@ -72,26 +72,23 @@ protected String patch(String url, String jsonBody) throws ApiException, AuthExc

try {
HttpResponse<String> response = Unirest.patch(url).basicAuth(this.apiKey, "")
.header("accept", Connection.Accept).header("Content-Type", "application/json").body(jsonBody)
.asString();
.header("accept", Connection.Accept).header("Content-Type", "application/json").body(jsonBody)
.asString();
responseString = response.getBody().toString();
responseCode = response.getStatus();

} catch (Throwable c) {
throw new ConnException(c);
}

if (responseCode == 401) {
throw new AuthException(responseString);
} else if (responseCode == 400 || responseCode == 403 || responseCode == 404) {
throw new ApiException(responseString);
if (responseCode >= 400) {
throw this.handleApiError(responseCode, responseString);
}

return responseString;
}

protected String get(String url, HashMap<String, Object> queryParms)
throws ApiException, AuthException, ConnException {
protected String get(String url, HashMap<String, Object> queryParms) throws InvoicedException {

String responseString = "";
int responseCode = -1;
Expand All @@ -102,8 +99,8 @@ protected String get(String url, HashMap<String, Object> queryParms)

try {
HttpResponse<String> response = Unirest.get(url).basicAuth(this.apiKey, "")
.header("accept", Connection.Accept).header("Content-Type", "application/json")
.queryString(queryParms).asString();
.header("accept", Connection.Accept).header("Content-Type", "application/json")
.queryString(queryParms).asString();

responseString = response.getBody().toString();
responseCode = response.getStatus();
Expand All @@ -112,19 +109,14 @@ protected String get(String url, HashMap<String, Object> queryParms)
throw new ConnException(c);
}

if (responseCode == 401) {
throw new AuthException(responseString);
} else if (responseCode == 400 || responseCode == 403 || responseCode == 404) {
throw new ApiException(responseString);
} else if (responseCode != 200 && responseCode != 204 && responseCode != 204) {
throw new ApiException(responseString);
if (responseCode >= 400) {
throw this.handleApiError(responseCode, responseString);
}

return responseString;
}

protected ListResponse getList(String url, HashMap<String, Object> queryParms)
throws ApiException, AuthException, ConnException {
protected ListResponse getList(String url, HashMap<String, Object> queryParms) throws InvoicedException {

String responseString = "";
int responseCode = -1;
Expand All @@ -137,8 +129,8 @@ protected ListResponse getList(String url, HashMap<String, Object> queryParms)

try {
HttpResponse<String> response = Unirest.get(url).basicAuth(this.apiKey, "")
.header("accept", Connection.Accept).header("Content-Type", "application/json")
.queryString(queryParms).asString();
.header("accept", Connection.Accept).header("Content-Type", "application/json")
.queryString(queryParms).asString();

responseString = response.getBody().toString();
responseCode = response.getStatus();
Expand All @@ -155,16 +147,14 @@ protected ListResponse getList(String url, HashMap<String, Object> queryParms)
throw new ConnException(c);
}

if (responseCode == 401) {
throw new AuthException(responseString);
} else if (responseCode == 400 || responseCode == 403 || responseCode == 404) {
throw new ApiException(responseString);
if (responseCode >= 400) {
throw this.handleApiError(responseCode, responseString);
}

return apiResult;
}

protected void delete(String url) throws ApiException, AuthException, ConnException {
protected void delete(String url) throws InvoicedException {

int responseCode = -1;
String responseString = "";
Expand All @@ -175,7 +165,7 @@ protected void delete(String url) throws ApiException, AuthException, ConnExcept

try {
HttpResponse<String> response = Unirest.delete(url).basicAuth(this.apiKey, "")
.header("accept", Connection.Accept).header("Content-Type", "application/json").asString();
.header("accept", Connection.Accept).header("Content-Type", "application/json").asString();

responseCode = response.getStatus();

Expand All @@ -187,14 +177,8 @@ protected void delete(String url) throws ApiException, AuthException, ConnExcept
throw new ConnException(c);
}

if (responseCode == 401) {
throw new AuthException(responseString);
} else if (responseCode == 400 || responseCode == 403 || responseCode == 404) {
throw new ApiException(responseString);
}

if (responseCode != 204) {
throw new ApiException("Object not deleted");
if (responseCode >= 400) {
throw this.handleApiError(responseCode, responseString);
}
}

Expand Down Expand Up @@ -258,4 +242,15 @@ protected final String baseUrl() {
return baseEndPointProduction;
}

protected final InvoicedException handleApiError(int responseCode, String responseBody) {
if (responseCode == 401) {
return new AuthException(responseBody);
} else if (responseCode == 400) {
return new InvalidRequestException(responseBody);
} else if (responseCode == 429) {
return new RateLimitException(responseBody);
}

return new ApiException(responseBody);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/invoiced/exception/ApiException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.invoiced.exception;

public class ApiException extends Exception {
public class ApiException extends InvoicedException {

private static final long serialVersionUID = 1L;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/invoiced/exception/AuthException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.invoiced.exception;

public class AuthException extends Exception {
public class AuthException extends InvoicedException {

private static final long serialVersionUID = 1L;

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/invoiced/exception/ConnException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.invoiced.exception;

public class ConnException extends Exception {
public class ConnException extends InvoicedException {

private static final long serialVersionUID = 1L;

public ConnException(Throwable cause) {
super(cause);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/invoiced/exception/EntityException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.invoiced.exception;

public class EntityException extends Exception {
public class EntityException extends InvoicedException {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.invoiced.exception;

public class InvalidRequestException extends InvoicedException {
private static final long serialVersionUID = 1L;

public InvalidRequestException(String message) {
super(message);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/invoiced/exception/InvoicedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.invoiced.exception;

abstract public class InvoicedException extends Exception {
private static final long serialVersionUID = 1L;

public InvoicedException(Throwable cause) {
super(cause);
}

public InvoicedException(String message) {
super(message);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/invoiced/exception/RateLimitException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.invoiced.exception;

public class RateLimitException extends InvoicedException {
private static final long serialVersionUID = 1L;

public RateLimitException(String message) {
super(message);
}
}
Loading

0 comments on commit c888e55

Please sign in to comment.