Skip to content

Commit

Permalink
fix: Catch unexpected null pointer exception (#70)
Browse files Browse the repository at this point in the history
### Summary

Catch unexpected null pointer exception and log error
retry on null response

### Checklist
* [x]Does your PR title have the correct title format?
* Does your PR have a breaking change?: no
  • Loading branch information
bohan-amplitude authored May 18, 2022
1 parent 06743fa commit ce5da03
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARTIFACT_VERSION=1.9.0
ARTIFACT_VERSION=1.9.1
ARTIFACT_GROUP=com.amplitude
POM_PACKAGING=jar
POM_DESCRIPTION=Amplitude Java SDK
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amplitude/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface Constants {

int NETWORK_TIMEOUT_MILLIS = 10000;
String SDK_LIBRARY = "amplitude-java";
String SDK_VERSION = "1.9.0";
String SDK_VERSION = "1.9.1";

int MAX_PROPERTY_KEYS = 1024;
int MAX_STRING_LENGTH = 1000;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/amplitude/HttpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public void sendEventsWithRetry(List<Event> events) {
sendEvents(events)
.thenAcceptAsync(
response -> {
if (response == null) {
logger.debug("Unexpected null response", "Retry events.");
retryEvents(events, new Response());
}
Status status = response.status;
if (shouldRetryForStatus(status)) {
retryEvents(events, response);
Expand Down Expand Up @@ -104,6 +108,8 @@ private CompletableFuture<Response> sendEvents(List<Event> events) {
logger.debug("SEND", events, response);
} catch (AmplitudeInvalidAPIKeyException e) {
throw new CompletionException(e);
} catch (Exception e) {
logger.error("Unexpected exception", Utils.getStackTrace(e));
}
return response;
});
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/amplitude/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Utils {
public static String getStringValueWithKey(JSONObject json, String key) {
return json.has(key) && json.getString(key) != null ? json.getString(key) : "";
Expand Down Expand Up @@ -32,4 +35,11 @@ public static int[] convertJSONArrayToIntArray(JSONObject json, String key) {
public static boolean isEmptyString(String s) {
return (s == null || s.length() == 0);
}

public static String getStackTrace(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}
5 changes: 3 additions & 2 deletions src/test/java/com/amplitude/util/MockURLStreamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

public class MockURLStreamHandler extends URLStreamHandler implements URLStreamHandlerFactory {
Expand All @@ -26,9 +27,9 @@ protected URLConnection openConnection(URL url) throws IOException {
protected URLConnection openConnection(URL u, Proxy p) throws IOException {
URLConnection connection = connections.get(u);
if (p.equals(Proxy.NO_PROXY)) {
when(((HttpURLConnection) connection).usingProxy()).thenReturn(false);
lenient().when(((HttpURLConnection) connection).usingProxy()).thenReturn(false);
} else {
when(((HttpURLConnection) connection).usingProxy()).thenReturn(true);
lenient().when(((HttpURLConnection) connection).usingProxy()).thenReturn(true);
}
return connection;
}
Expand Down

0 comments on commit ce5da03

Please sign in to comment.