Skip to content

Commit

Permalink
Merge pull request #36 from taxjar/XCON-33-Handle-Null-Error-Message
Browse files Browse the repository at this point in the history
Fix Null Request Body in Exception and handle 500 Errors
  • Loading branch information
dallendalton authored Mar 8, 2022
2 parents 479ece9 + cc3a641 commit 9b2f4a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/main/java/com/taxjar/exception/TaxjarException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
public class TaxjarException extends Exception {
private Integer statusCode;

public TaxjarException(String errorMessage, int statusCode) {
super(parseMessage(errorMessage), null);
this.statusCode = parseStatusCode(errorMessage, statusCode);
}

public TaxjarException(String errorMessage) {
this(errorMessage, null);
}
Expand All @@ -24,6 +29,8 @@ public Integer getStatusCode() {
private static String parseMessage(String errorMessage) {
Gson gson = new Gson();

if (errorMessage == null || errorMessage.equals("")) return "";

try {
JsonObject json = gson.fromJson(errorMessage, JsonObject.class);
JsonElement error = json.get("error");
Expand All @@ -39,9 +46,19 @@ private static String parseMessage(String errorMessage) {
}
}

private static Integer parseStatusCode(String errorMessage, int statusCode) {
if (errorMessage == null || errorMessage.equals("")) {
return statusCode;
} else {
return parseStatusCode(errorMessage);
}
}

private static Integer parseStatusCode(String errorMessage) {
Gson gson = new Gson();

if (errorMessage == null) return 0;

try {
JsonObject json = gson.fromJson(errorMessage, JsonObject.class);
JsonElement status = json.get("status");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/taxjar/net/ApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public T execute() throws TaxjarException {
if (response.isSuccessful()) {
return response.body();
} else {
throw new TaxjarException(response.errorBody().string());
throw new TaxjarException(response.errorBody().string(), response.code());
}
} catch (IOException e) {
throw new ApiConnectionException(e.getMessage(), e);
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/taxjar/exception/ExceptionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.taxjar.exception;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.taxjar.MockInterceptor;
import com.taxjar.Taxjar;
import com.taxjar.TaxjarMock;
Expand Down Expand Up @@ -151,4 +153,36 @@ public void testEmptyJson() {
assertEquals(json, e.getMessage());
assertEquals((Integer) 0, e.getStatusCode());
}

public void testNullErrorMessage() {
TaxjarException e = new TaxjarException(null);

assertEquals("", e.getMessage());
assertEquals((Integer) 0, e.getStatusCode());
}

public void test500Response() throws IOException {
TaxjarException e = null;
MockWebServer server = new MockWebServer();
MockResponse response = new MockResponse().setResponseCode(500);

server.enqueue(response);
server.start();

Map<String, Object> params = new HashMap<>();
params.put("apiUrl", "http://" + server.getHostName() + ":" + server.getPort());
params.put("timeout", 500);
Taxjar client = new Taxjar("TEST", params);

try {
CategoryResponse res = client.categories();
} catch (TaxjarException ex) {
e = ex;
}

assertEquals("", e.getMessage());
assertEquals((Integer) 500, e.getStatusCode());

server.shutdown();
}
}

0 comments on commit 9b2f4a7

Please sign in to comment.