Skip to content

Commit 8c7ac41

Browse files
committed
chore: use promises in fatal crash reporting and add logs
1 parent b9aeb94 commit 8c7ac41

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

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: 16 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,16 @@ 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+
console.log('IBG-RN: Captured a JavaScript error', err);
76+
77+
await instabugErrorHandler(err, isFatal);
78+
79+
console.log('IBG-RN: Reported the JavaScript error to Instabug', err);
7680

7781
if (process.env.JEST_WORKER_ID) {
7882
return;
@@ -86,6 +90,8 @@ export const captureJsErrors = () => {
8690
originalErrorHandler(err, isFatal);
8791
}
8892
});
93+
94+
console.log('IBG-RN: Global error handler is now set.');
8995
};
9096

9197
export const stringifyIfNotString = (input: unknown) => {
@@ -104,9 +110,9 @@ export const stringifyIfNotString = (input: unknown) => {
104110
* `sendCrashReport(error, NativeCrashReporting.sendJSCrash);`
105111
*
106112
*/
107-
export function sendCrashReport(
113+
export async function sendCrashReport(
108114
error: ExtendedError,
109-
remoteSenderCallback: (json: CrashData | string) => void,
115+
remoteSenderCallback: (json: CrashData | string) => Promise<void>,
110116
) {
111117
const jsStackTrace = getStackTrace(error);
112118

@@ -120,10 +126,10 @@ export function sendCrashReport(
120126
};
121127

122128
if (Platform.OS === 'android') {
123-
remoteSenderCallback(JSON.stringify(jsonObject));
124-
} else {
125-
remoteSenderCallback(jsonObject);
129+
return remoteSenderCallback(JSON.stringify(jsonObject));
126130
}
131+
132+
return remoteSenderCallback(jsonObject);
127133
}
128134

129135
export default {

0 commit comments

Comments
 (0)