Skip to content

Commit eff9609

Browse files
a7medevHeshamMegid
authored andcommitted
chore(ios): sync JavaScript fatal crashes through promises (#1054)
Jira ID: MOB-13245
1 parent 5d84038 commit eff9609

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
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 Android Gradle Plugin namespace support required for React Native 0.73 and backward compatibility with previous versions ([#1044](https://github.com/Instabug/Instabug-React-Native/pull/1044)).
8+
- Fix an issue with unhandled JavaScript crashes being reported as native iOS crashes ([#1054](https://github.com/Instabug/Instabug-React-Native/pull/1054))
89

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

ios/RNInstabug/InstabugCrashReportingBridge.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,29 @@ + (BOOL)requiresMainQueueSetup
2626
IBGCrashReporting.enabled = isEnabled;
2727
}
2828

29-
RCT_EXPORT_METHOD(sendJSCrash:(NSDictionary *)stackTrace) {
29+
RCT_EXPORT_METHOD(sendJSCrash:(NSDictionary *)stackTrace
30+
resolver:(RCTPromiseResolveBlock)resolve
31+
rejecter:(RCTPromiseRejectBlock)reject) {
3032
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
3133
dispatch_async(queue, ^{
3234
SEL reportCrashWithStackTraceSEL = NSSelectorFromString(@"reportCrashWithStackTrace:handled:");
3335
if ([[Instabug class] respondsToSelector:reportCrashWithStackTraceSEL]) {
3436
[[Instabug class] performSelector:reportCrashWithStackTraceSEL withObject:stackTrace withObject:@(NO)];
3537
}
38+
resolve([NSNull null]);
3639
});
3740
}
3841

39-
RCT_EXPORT_METHOD(sendHandledJSCrash:(NSDictionary *)stackTrace) {
42+
RCT_EXPORT_METHOD(sendHandledJSCrash:(NSDictionary *)stackTrace
43+
resolver:(RCTPromiseResolveBlock)resolve
44+
rejecter:(RCTPromiseRejectBlock)reject) {
4045
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
4146
dispatch_async(queue, ^{
4247
SEL reportCrashWithStackTraceSEL = NSSelectorFromString(@"reportCrashWithStackTrace:handled:");
4348
if ([[Instabug class] respondsToSelector:reportCrashWithStackTraceSEL]) {
4449
[[Instabug class] performSelector:reportCrashWithStackTraceSEL withObject:stackTrace withObject:@(YES)];
4550
}
51+
resolve([NSNull null]);
4652
});
4753
}
4854

src/modules/CrashReporting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ export const setEnabled = (isEnabled: boolean) => {
1717
* @param error Error object to be sent to Instabug's servers
1818
*/
1919
export const reportError = (error: ExtendedError) => {
20-
InstabugUtils.sendCrashReport(error, NativeCrashReporting.sendHandledJSCrash);
20+
return InstabugUtils.sendCrashReport(error, NativeCrashReporting.sendHandledJSCrash);
2121
};

src/native/NativeCrashReporting.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface CrashData {
1414

1515
export interface CrashReportingNativeModule extends NativeModule {
1616
setEnabled(isEnabled: boolean): void;
17-
sendJSCrash(data: CrashData | string): void;
18-
sendHandledJSCrash(data: CrashData | string): void;
17+
sendJSCrash(data: CrashData | string): Promise<void>;
18+
sendHandledJSCrash(data: CrashData | string): Promise<void>;
1919
}
2020

2121
export const NativeCrashReporting = NativeModules.IBGCrashReporting;

src/utils/InstabugUtils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorHandlerCallback, Platform } from 'react-native';
1+
import { Platform } from 'react-native';
22
import parseErrorStackLib, {
33
ExtendedError,
44
StackFrame,
@@ -67,12 +67,12 @@ export const captureJsErrors = () => {
6767

6868
const originalErrorHandler = ErrorUtils.getGlobalHandler();
6969

70-
const instabugErrorHandler: ErrorHandlerCallback = (err) => {
71-
sendCrashReport(err, NativeCrashReporting.sendJSCrash);
70+
const instabugErrorHandler = (err: any, _isFatal?: boolean): Promise<void> => {
71+
return sendCrashReport(err, NativeCrashReporting.sendJSCrash);
7272
};
7373

74-
ErrorUtils.setGlobalHandler((err, isFatal) => {
75-
instabugErrorHandler(err, isFatal);
74+
ErrorUtils.setGlobalHandler(async (err, isFatal) => {
75+
await instabugErrorHandler(err, isFatal);
7676

7777
if (process.env.JEST_WORKER_ID) {
7878
return;
@@ -104,9 +104,9 @@ export const stringifyIfNotString = (input: unknown) => {
104104
* `sendCrashReport(error, NativeCrashReporting.sendJSCrash);`
105105
*
106106
*/
107-
export function sendCrashReport(
107+
export async function sendCrashReport(
108108
error: ExtendedError,
109-
remoteSenderCallback: (json: CrashData | string) => void,
109+
remoteSenderCallback: (json: CrashData | string) => Promise<void>,
110110
) {
111111
const jsStackTrace = getStackTrace(error);
112112

@@ -120,10 +120,10 @@ export function sendCrashReport(
120120
};
121121

122122
if (Platform.OS === 'android') {
123-
remoteSenderCallback(JSON.stringify(jsonObject));
124-
} else {
125-
remoteSenderCallback(jsonObject);
123+
return remoteSenderCallback(JSON.stringify(jsonObject));
126124
}
125+
126+
return remoteSenderCallback(jsonObject);
127127
}
128128

129129
export default {

0 commit comments

Comments
 (0)