Skip to content

Commit 109e071

Browse files
authored
E2-1372 : Add Exception handler code (#202)
1 parent ada935e commit 109e071

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ protected Void doInBackground(Void... params) {
581581
return null;
582582
}
583583
});
584+
585+
Util.initExceptionHandling(context);
584586
} catch (Throwable t) {
585587
Util.handleException(t);
586588
}

AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.leanplum.LeanplumException;
4848
import com.leanplum.internal.Constants.Methods;
4949
import com.leanplum.internal.Constants.Params;
50+
import com.leanplum.monitoring.ExceptionHandler;
5051
import com.leanplum.utils.SharedPreferencesUtil;
5152

5253
import org.json.JSONException;
@@ -837,10 +838,19 @@ private static void setUpdateTime(Map<String, Object> params, PackageManager pac
837838
}
838839
}
839840

841+
/**
842+
* Initialize exception handling in the SDK.
843+
*/
844+
public static void initExceptionHandling(Context context) {
845+
ExceptionHandler.getInstance().setContext(context);
846+
}
847+
840848
/**
841849
* Handles uncaught exceptions in the SDK.
842850
*/
843851
public static void handleException(Throwable t) {
852+
ExceptionHandler.getInstance().reportException(t);
853+
844854
if (t instanceof OutOfMemoryError) {
845855
if (Constants.isDevelopmentModeEnabled) {
846856
throw (OutOfMemoryError) t;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.leanplum.monitoring;
2+
3+
import android.content.Context;
4+
5+
import com.leanplum.internal.Log;
6+
7+
public class ExceptionHandler {
8+
private static final String LEANPLUM_CRASH_REPORTER_CLASS =
9+
"com.leanplum.monitoring.internal.LeanplumExceptionReporter";
10+
private static final ExceptionHandler instance = new ExceptionHandler();
11+
public ExceptionReporting exceptionReporter = null;
12+
13+
private ExceptionHandler() {}
14+
15+
public static ExceptionHandler getInstance() {
16+
return instance;
17+
}
18+
19+
public void setContext(Context context) {
20+
try {
21+
// Class.forName runs the static initializer in LeanplumExceptionReporter
22+
// which sets the exceptionReporter on the singleton
23+
Class.forName(LEANPLUM_CRASH_REPORTER_CLASS);
24+
if (exceptionReporter != null) {
25+
try {
26+
exceptionReporter.setContext(context);
27+
} catch (Throwable t) {
28+
Log.e("LeanplumCrashHandler", t);
29+
}
30+
}
31+
} catch (Throwable t) {
32+
Log.e("LeanplumCrashHandler", t);
33+
}
34+
}
35+
36+
public void reportException(Throwable exception) {
37+
if (exceptionReporter != null) {
38+
try {
39+
exceptionReporter.reportException(exception);
40+
} catch (Throwable t) {
41+
Log.e("LeanplumCrashHandler", t);
42+
}
43+
}
44+
}
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.leanplum.monitoring;
2+
3+
import android.content.Context;
4+
5+
public interface ExceptionReporting {
6+
void setContext(Context context);
7+
void reportException(Throwable t);
8+
}

0 commit comments

Comments
 (0)