Skip to content

Commit

Permalink
Merge pull request #17 from avohq/handle-exceptions-in-network-logic
Browse files Browse the repository at this point in the history
Handle errors in the connection logic
  • Loading branch information
aleks-tpom6oh authored Nov 26, 2020
2 parents adb8ee3 + dbda78b commit ea66118
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 40 deletions.
6 changes: 3 additions & 3 deletions avoinspector/src/main/java/app/avo/inspector/AvoBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ public void run() {

batchFlushAttemptMillis = System.currentTimeMillis();

final List<Map<String, Object>> sendingEvents = events;
final List<Map<String, Object>> sendingEvents = new ArrayList<>(events);
events = Collections.synchronizedList(new ArrayList<Map<String, Object>>());

networkCallsHandler.reportInspectorWithBatchBody(sendingEvents,
new AvoNetworkCallsHandler.Callback() {
@Override
public void call(@Nullable String error) {
public void call(boolean retry) {
if (clearCache) {
sharedPrefs.edit().remove(avoInspectorBatchKey).apply();
}

if (error != null) {
if (retry) {
events.addAll(sendingEvents);
}
}
Expand Down
38 changes: 12 additions & 26 deletions avoinspector/src/main/java/app/avo/inspector/AvoInspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -30,11 +29,10 @@
import app.avo.androidanalyticsdebugger.PropertyError;
import io.sentry.DefaultSentryClientFactory;
import io.sentry.Sentry;
import io.sentry.SentryClient;
import io.sentry.SentryClientFactory;
import io.sentry.config.Lookup;
import io.sentry.config.provider.ConfigurationProvider;
import io.sentry.dsn.Dsn;

import static app.avo.inspector.Util.handleException;

public class AvoInspector implements Inspector {

Expand Down Expand Up @@ -121,12 +119,12 @@ public void onActivityStarted(@NonNull Activity activity) {
try {
avoBatcher.enterForeground();
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
}
try {
sessionTracker.startOrProlongSession(System.currentTimeMillis());
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
}
}
}
Expand All @@ -140,7 +138,7 @@ public void onTrimMemory(int i) {
try {
avoBatcher.enterBackground();
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
}
}
}
Expand All @@ -153,18 +151,6 @@ public void onLowMemory() {}
});
}

private void handleException(Exception e) {
try {
Sentry.capture(e);
} catch (Throwable throwable) {
Log.e("Avo Inspector", "Failed to report a crash. Please report to support@avo.app.", e);
}
if (AvoInspectorEnv.Dev.getName().equals(this.env)) {
Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e);
throw new RuntimeException(e);
}
}

private void setupSentry(String appVersionString) {
try {
Lookup lookup = Lookup.getDefaultWithAdditionalProviders(Collections.<ConfigurationProvider>singletonList(new ConfigurationProvider() {
Expand Down Expand Up @@ -196,7 +182,7 @@ public void showVisualInspector(Activity rootActivity, DebuggerMode visualInspec
}
debugger.showDebugger(rootActivity, visualInspectorMode);
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
}
}

Expand All @@ -207,7 +193,7 @@ public void hideVisualInspector(Activity rootActivity) {
debugger.hideDebugger(rootActivity);
}
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
}
}

Expand All @@ -231,7 +217,7 @@ public void hideVisualInspector(Activity rootActivity) {
return new HashMap<>();
}
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
return new HashMap<>();
}
}
Expand All @@ -255,7 +241,7 @@ public void hideVisualInspector(Activity rootActivity) {
return new HashMap<>();
}
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
return new HashMap<>();
}
}
Expand All @@ -279,7 +265,7 @@ public void hideVisualInspector(Activity rootActivity) {
return new HashMap<>();
}
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
return new HashMap<>();
}
}
Expand Down Expand Up @@ -360,7 +346,7 @@ public void trackSchema(@NonNull String eventName, @Nullable Map<String, AvoEven
}
}
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
}
}

Expand Down Expand Up @@ -421,7 +407,7 @@ static void logPostExtract(@Nullable String eventName, @NonNull Map<String, AvoE

return avoSchemaExtractor.extractSchema(eventProperties, true);
} catch (Exception e) {
handleException(e);
handleException(e, AvoInspector.this.env);
return new HashMap<>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -143,7 +144,7 @@ public void run() {
callbackHandler.post(new Runnable() {
@Override
public void run() {
completionHandler.call("HTTP error code: " + responseCode);
completionHandler.call(true);
}
});
} else {
Expand Down Expand Up @@ -178,7 +179,7 @@ public void run() {
callbackHandler.post(new Runnable() {
@Override
public void run() {
completionHandler.call(null);
completionHandler.call(false);
}
});
}
Expand All @@ -191,17 +192,20 @@ public void run() {
if (AvoInspector.isLogging()) {
Log.e("AvoInspector", "Failed to perform network call, will retry later");
}
completionHandler.call("Failed to perform network call");
completionHandler.call(true);
} catch (Exception e) {
Util.handleException(e, envName);
completionHandler.call(false);
}
}
}).start();

}

private void writeTrackingCallBody(List<Map<String, Object>> data, HttpsURLConnection connection) throws IOException {

JSONArray body = new JSONArray();
for (Map<String, Object> event: data) {
for (Iterator<Map<String, Object>> iterator = data.iterator(); iterator.hasNext(); ) {
Map<String, Object> event = iterator.next();
JSONObject eventJson = new JSONObject(event);
body.put(eventJson);
}
Expand All @@ -220,6 +224,6 @@ private void writeTrackingCallHeader(HttpsURLConnection connection) {
}

interface Callback {
void call(@Nullable String error);
void call(boolean retry);
}
}
16 changes: 16 additions & 0 deletions avoinspector/src/main/java/app/avo/inspector/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app.avo.inspector;

import android.util.Log;

import androidx.annotation.Nullable;

import org.json.JSONArray;
Expand All @@ -18,6 +20,8 @@
import java.util.Map;
import java.util.TimeZone;

import io.sentry.Sentry;

class Util {

static final String AVO_SHARED_PREFS_KEY = "avo_inspector_preferences";
Expand Down Expand Up @@ -185,4 +189,16 @@ private static boolean arraysEqual(@Nullable Object paramValue, @Nullable Object
}
return false;
}

static void handleException(Throwable e, String envName) {
try {
Sentry.capture(e);
} catch (Throwable throwable) {
Log.e("Avo Inspector", "Failed to report a crash. Please report to support@avo.app.", e);
}
if (AvoInspectorEnv.Dev.getName().equals(envName)) {
Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e);
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void saveUpTo1000EventsOnBackgroundAndRestoresOnForeground() throws Inter
networkCallbackCaptor.capture());

// When
networkCallbackCaptor.getValue().call(null);
networkCallbackCaptor.getValue().call(false);

// Then
assertEquals(1000, listCaptor.getValue().size());
Expand Down Expand Up @@ -252,7 +252,7 @@ public void restoresEventsOnFailedNetworkPost() throws InterruptedException {
assertEquals(0, sut.events.size());

// When
networkCallbackCaptor.getValue().call("Failed");
networkCallbackCaptor.getValue().call(true);

assertEquals(AvoBatcher.batchSize, sut.events.size());
}
Expand Down Expand Up @@ -292,7 +292,7 @@ public void clearsEventsOnSuccessNetworkPost() throws InterruptedException {
assertEquals(0, sut.events.size());

// When
networkCallbackCaptor.getValue().call(null);
networkCallbackCaptor.getValue().call(false);

// Then
assertEquals(0, sut.events.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void doesNotSendDataWithSamplingRate0() throws InterruptedException {
}},
new AvoNetworkCallsHandler.Callback() {
@Override
public void call(@Nullable String error) {
public void call(boolean retry) {
}
});
}
Expand All @@ -63,7 +63,7 @@ public void alwaysSendsDataWithSamplingRate1() throws InterruptedException {
}},
new AvoNetworkCallsHandler.Callback() {
@Override
public void call(@Nullable String error) {
public void call(boolean retry) {
Assert.fail();
}
});
Expand Down

0 comments on commit ea66118

Please sign in to comment.