Skip to content

Commit 8008232

Browse files
abdelhamid-f-nasserHeshamMegid
authored andcommitted
chore(android): sync JS fatal crashes via promises (#1071)
Jira ID: MOB-13312
1 parent 55a7b23 commit 8008232

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Fix an issue with `Instabug.init` on Android causing the app to crash while trying to get the current `Application` instance through the current activity which can be `null` in some cases by utilizing the React context instead ([#1069](https://github.com/Instabug/Instabug-React-Native/pull/1069)).
8+
- Fix an issue with unhandled JavaScript crashes not getting linked with the current session causing inaccurate session metrics ([#1071](https://github.com/Instabug/Instabug-React-Native/pull/1071)).
89

910
## [12.2.0](https://github.com/Instabug/Instabug-React-Native/compare/v12.2.0...v12.1.0)
1011

android/src/main/java/com/instabug/reactlibrary/RNInstabugCrashReportingModule.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;
44

5+
import com.facebook.react.bridge.Promise;
56
import com.facebook.react.bridge.ReactApplicationContext;
67
import com.facebook.react.bridge.ReactContextBaseJavaModule;
78
import com.facebook.react.bridge.ReactMethod;
@@ -15,6 +16,7 @@
1516
import java.lang.reflect.Method;
1617

1718
import javax.annotation.Nonnull;
19+
import javax.annotation.Nullable;
1820

1921
public class RNInstabugCrashReportingModule extends ReactContextBaseJavaModule {
2022

@@ -55,12 +57,19 @@ public void run() {
5557
* Send unhandled JS error object
5658
*
5759
* @param exceptionObject Exception object to be sent to Instabug's servers
60+
* @param promise This makes sure that the RN side crashes the app only after the Android SDK
61+
* finishes processing/handling the crash.
5862
*/
5963
@ReactMethod
60-
public void sendJSCrash(final String exceptionObject) {
64+
public void sendJSCrash(final String exceptionObject, final Promise promise) {
6165
try {
6266
JSONObject jsonObject = new JSONObject(exceptionObject);
63-
sendJSCrashByReflection(jsonObject, false);
67+
sendJSCrashByReflection(jsonObject, false, new Runnable() {
68+
@Override
69+
public void run() {
70+
promise.resolve(null);
71+
}
72+
});
6473
} catch (Exception e) {
6574
e.printStackTrace();
6675
}
@@ -75,13 +84,13 @@ public void sendJSCrash(final String exceptionObject) {
7584
public void sendHandledJSCrash(final String exceptionObject) {
7685
try {
7786
JSONObject jsonObject = new JSONObject(exceptionObject);
78-
sendJSCrashByReflection(jsonObject, true);
87+
sendJSCrashByReflection(jsonObject, true, null);
7988
} catch (Exception e) {
8089
e.printStackTrace();
8190
}
8291
}
8392

84-
private void sendJSCrashByReflection(final JSONObject exceptionObject, final boolean isHandled) {
93+
private void sendJSCrashByReflection(final JSONObject exceptionObject, final boolean isHandled, @Nullable final Runnable onComplete) {
8594
MainThreadHandler.runOnMainThread(new Runnable() {
8695
@Override
8796
public void run() {
@@ -97,6 +106,10 @@ public void run() {
97106
e.printStackTrace();
98107
} catch (InvocationTargetException e) {
99108
e.printStackTrace();
109+
} finally {
110+
if (onComplete != null) {
111+
onComplete.run();
112+
}
100113
}
101114
}
102115
});

src/utils/InstabugUtils.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,7 @@ export const captureJsErrors = () => {
7777
if (process.env.JEST_WORKER_ID) {
7878
return;
7979
}
80-
81-
if (Platform.OS === 'android') {
82-
setTimeout(() => {
83-
originalErrorHandler(err, isFatal);
84-
}, 500);
85-
} else {
86-
originalErrorHandler(err, isFatal);
87-
}
80+
originalErrorHandler(err, isFatal);
8881
});
8982
};
9083

0 commit comments

Comments
 (0)