Skip to content

Commit 7e56452

Browse files
a7medevymabdallah
authored andcommitted
[MOB-11900] Remove Unneeded Crash Reporting Events (#916)
1 parent 26b7fae commit 7e56452

8 files changed

+22
-261
lines changed

src/modules/CrashReporting.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { Platform } from 'react-native';
22
import type { ExtendedError } from 'react-native/Libraries/Core/Devtools/parseErrorStack';
33

44
import { NativeCrashReporting } from '../native';
5-
import IBGEventEmitter from '../utils/IBGEventEmitter';
6-
import InstabugConstants from '../utils/InstabugConstants';
75
import InstabugUtils from '../utils/InstabugUtils';
86

97
/**
@@ -42,13 +40,9 @@ export const reportError = (error: ExtendedError) => {
4240
exception: jsStackTrace,
4341
};
4442

45-
if (InstabugUtils.isOnReportHandlerSet() && Platform.OS === 'android') {
46-
IBGEventEmitter.emit(InstabugConstants.SEND_HANDLED_CRASH, jsonObject);
43+
if (Platform.OS === 'android') {
44+
NativeCrashReporting.sendHandledJSCrash(JSON.stringify(jsonObject));
4745
} else {
48-
if (Platform.OS === 'android') {
49-
NativeCrashReporting.sendHandledJSCrash(JSON.stringify(jsonObject));
50-
} else {
51-
NativeCrashReporting.sendHandledJSCrash(jsonObject);
52-
}
46+
NativeCrashReporting.sendHandledJSCrash(jsonObject);
5347
}
5448
};

src/modules/Instabug.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { NavigationAction, NavigationState as NavigationStateV4 } from 'rea
77

88
import type { InstabugConfig } from '../models/InstabugConfig';
99
import Report from '../models/Report';
10-
import { NativeCrashReporting, NativeInstabug } from '../native';
10+
import { NativeInstabug } from '../native';
1111
import {
1212
IBGPosition,
1313
actionTypes,
@@ -533,64 +533,12 @@ export const show = () => {
533533
};
534534

535535
export const onReportSubmitHandler = (handler?: (report: Report) => void) => {
536-
if (handler) {
537-
InstabugUtils.setOnReportHandler(true);
538-
} else {
539-
InstabugUtils.setOnReportHandler(false);
540-
}
541-
542-
// send bug report
543536
IBGEventEmitter.addListener(NativeInstabug, InstabugConstants.PRESENDING_HANDLER, (report) => {
544537
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
545538
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
546539
handler && handler(reportObj);
547540
});
548541

549-
// handled js crash
550-
if (Platform.OS === 'android') {
551-
IBGEventEmitter.addListener(
552-
NativeInstabug,
553-
InstabugConstants.SEND_HANDLED_CRASH,
554-
async (jsonObject) => {
555-
try {
556-
const report = await NativeInstabug.getReport();
557-
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
558-
const reportObj = new Report(
559-
tags,
560-
consoleLogs,
561-
instabugLogs,
562-
userAttributes,
563-
fileAttachments,
564-
);
565-
handler && handler(reportObj);
566-
NativeCrashReporting.sendHandledJSCrash(JSON.stringify(jsonObject));
567-
} catch (e) {
568-
console.error(e);
569-
}
570-
},
571-
);
572-
}
573-
574-
if (Platform.OS === 'android') {
575-
IBGEventEmitter.addListener(
576-
NativeInstabug,
577-
InstabugConstants.SEND_UNHANDLED_CRASH,
578-
async (jsonObject) => {
579-
const report = await NativeInstabug.getReport();
580-
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
581-
const reportObj = new Report(
582-
tags,
583-
consoleLogs,
584-
instabugLogs,
585-
userAttributes,
586-
fileAttachments,
587-
);
588-
handler && handler(reportObj);
589-
NativeCrashReporting.sendJSCrash(JSON.stringify(jsonObject));
590-
},
591-
);
592-
}
593-
594542
NativeInstabug.setPreSendingHandler(handler);
595543
};
596544

src/utils/InstabugConstants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
enum InstabugConstants {
22
NETWORK_DATA_OBFUSCATION_HANDLER_EVENT = 'IBGSetNetworkDataObfuscationHandler',
33
PRESENDING_HANDLER = 'IBGpreSendingHandler',
4-
SEND_HANDLED_CRASH = 'IBGSendHandledJSCrash',
5-
SEND_UNHANDLED_CRASH = 'IBGSendUnhandledJSCrash',
64
ON_INVOKE_HANDLER = 'IBGpreInvocationHandler',
75
ON_SDK_DISMISSED_HANDLER = 'IBGpostInvocationHandler',
86
ON_REPLY_RECEIVED_HANDLER = 'IBGOnNewReplyReceivedCallback',

src/utils/InstabugUtils.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@ import type { NavigationState as NavigationStateV5, PartialState } from '@react-
88
import type { NavigationState as NavigationStateV4 } from 'react-navigation';
99

1010
import { NativeCrashReporting } from '../native';
11-
import IBGEventEmitter from './IBGEventEmitter';
12-
import InstabugConstants from './InstabugConstants';
1311

1412
export const parseErrorStack = (error: ExtendedError): StackFrame[] => {
1513
return parseErrorStackLib(error);
1614
};
1715

18-
let _isOnReportHandlerSet = false;
19-
20-
export const isOnReportHandlerSet = (): boolean => _isOnReportHandlerSet;
21-
22-
export const setOnReportHandler = (flag: boolean) => {
23-
_isOnReportHandlerSet = flag;
24-
};
25-
2616
export const getActiveRouteName = (navigationState: NavigationStateV4): string | null => {
2717
if (!navigationState) {
2818
return null;
@@ -90,11 +80,7 @@ export const captureJsErrors = () => {
9080
};
9181

9282
if (Platform.OS === 'android') {
93-
if (_isOnReportHandlerSet) {
94-
IBGEventEmitter.emit(InstabugConstants.SEND_UNHANDLED_CRASH, jsonObject);
95-
} else {
96-
NativeCrashReporting.sendJSCrash(JSON.stringify(jsonObject));
97-
}
83+
NativeCrashReporting.sendJSCrash(JSON.stringify(jsonObject));
9884
} else {
9985
NativeCrashReporting.sendJSCrash(jsonObject);
10086
}
@@ -115,8 +101,6 @@ export const stringifyIfNotString = (input: unknown) => {
115101
export default {
116102
parseErrorStack,
117103
captureJsErrors,
118-
setOnReportHandler,
119-
isOnReportHandlerSet,
120104
getActiveRouteName,
121105
getFullRoute,
122106
getStackTrace,

test/mocks/mockInstabugUtils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ jest.mock('../../src/utils/InstabugUtils', () => {
22
return {
33
parseErrorStack: jest.fn(),
44
captureJsErrors: jest.fn(),
5-
setOnReportHandler: jest.fn(),
6-
isOnReportHandlerSet: jest.fn(),
75
getActiveRouteName: jest.fn(),
86
stringifyIfNotString: jest.fn(),
97
getStackTrace: jest.fn().mockReturnValue('javascriptStackTrace'),

test/modules/CrashReporting.spec.ts

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import '../mocks/mockInstabugUtils';
22

3-
import { NativeModules, Platform } from 'react-native';
3+
import { Platform } from 'react-native';
44

55
import * as CrashReporting from '../../src/modules/CrashReporting';
66
import { NativeCrashReporting } from '../../src/native';
7-
import IBGEventEmitter from '../../src/utils/IBGEventEmitter';
8-
import IBGConstants from '../../src/utils/InstabugConstants';
9-
import InstabugUtils from '../../src/utils/InstabugUtils';
107

118
describe('CrashReporting Module', () => {
129
it('should call the native method setEnabled', () => {
@@ -16,12 +13,12 @@ describe('CrashReporting Module', () => {
1613
expect(NativeCrashReporting.setEnabled).toBeCalledWith(true);
1714
});
1815

19-
it('should call the native method sendHandledJSCrash when platform is ios', () => {
16+
it('should call the native method sendHandledJSCrash with JSON object when platform is iOS', () => {
2017
Platform.OS = 'ios';
21-
const errorObject = { name: 'TypeError', message: 'Invalid type' };
22-
CrashReporting.reportError(errorObject);
18+
const error = { name: 'TypeError', message: 'Invalid type' };
19+
CrashReporting.reportError(error);
2320

24-
const expectedObject = {
21+
const expected = {
2522
message: 'TypeError - Invalid type',
2623
e_message: 'Invalid type',
2724
e_name: 'TypeError',
@@ -31,51 +28,24 @@ describe('CrashReporting Module', () => {
3128
};
3229

3330
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledTimes(1);
34-
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledWith(expectedObject);
31+
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledWith(expected);
3532
});
3633

37-
it('should call the native method sendHandledJSCrash when platform is android', () => {
34+
it('should call the native method sendHandledJSCrash with stringified JSON object when platform is Android', () => {
3835
Platform.OS = 'android';
39-
const errorObject = { name: 'TypeError', message: 'Invalid type' };
40-
CrashReporting.reportError(errorObject);
36+
const error = { name: 'TypeError', message: 'Invalid type' };
37+
CrashReporting.reportError(error);
4138

42-
const expectedObject = {
39+
const expected = JSON.stringify({
4340
message: 'TypeError - Invalid type',
4441
e_message: 'Invalid type',
4542
e_name: 'TypeError',
4643
os: 'android',
4744
platform: 'react_native',
4845
exception: 'javascriptStackTrace',
49-
};
46+
});
5047

5148
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledTimes(1);
52-
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledWith(JSON.stringify(expectedObject));
53-
});
54-
55-
//TODO: finish this
56-
it('should emit event IBGSendHandledJSCrash with the error object when platform is android', () => {
57-
Platform.OS = 'android';
58-
InstabugUtils.isOnReportHandlerSet = jest.fn().mockReturnValue(true);
59-
60-
const errorObject = { name: 'TypeError', message: 'Invalid type' };
61-
const expectedObject = {
62-
message: 'TypeError - Invalid type',
63-
e_message: 'Invalid type',
64-
e_name: 'TypeError',
65-
os: 'android',
66-
platform: 'react_native',
67-
exception: 'javascriptStackTrace',
68-
};
69-
70-
const crashHandler = jest.fn();
71-
IBGEventEmitter.addListener(
72-
NativeModules.Instabug,
73-
IBGConstants.SEND_HANDLED_CRASH,
74-
crashHandler,
75-
);
76-
77-
CrashReporting.reportError(errorObject);
78-
expect(crashHandler).toBeCalledTimes(1);
79-
expect(crashHandler).toBeCalledWith(expectedObject);
49+
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledWith(expected);
8050
});
8151
});

test/modules/Instabug.spec.ts

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import '../mocks/mockInstabugUtils';
22

3-
import { NativeModules, Platform, findNodeHandle, processColor } from 'react-native';
3+
import { Platform, findNodeHandle, processColor } from 'react-native';
44

55
import waitForExpect from 'wait-for-expect';
66

77
import Report from '../../src/models/Report';
88
import * as Instabug from '../../src/modules/Instabug';
9-
import { NativeCrashReporting, NativeInstabug } from '../../src/native';
9+
import { NativeInstabug } from '../../src/native';
1010
import {
1111
ColorTheme,
1212
InvocationEvent,
@@ -610,16 +610,6 @@ describe('Instabug Module', () => {
610610
expect(NativeInstabug.show).toBeCalledTimes(1);
611611
});
612612

613-
it('should set _isOnReportHandlerSet to true on calling onReportSubmitHandler', () => {
614-
Instabug.onReportSubmitHandler(jest.fn());
615-
expect(InstabugUtils.setOnReportHandler).toBeCalledWith(true);
616-
});
617-
618-
it('should set _isOnReportHandlerSet to false on calling onReportSubmitHandler without a handler', () => {
619-
Instabug.onReportSubmitHandler();
620-
expect(InstabugUtils.setOnReportHandler).toBeCalledWith(false);
621-
});
622-
623613
it('should call the native method setPreSendingHandler with a function', () => {
624614
const callback = jest.fn();
625615
Instabug.onReportSubmitHandler(callback);
@@ -651,97 +641,6 @@ describe('Instabug Module', () => {
651641
expect(IBGEventEmitter.getListeners(IBGConstants.PRESENDING_HANDLER).length).toEqual(1);
652642
});
653643

654-
it('should invoke callback on emitting the event IBGSendHandledJSCrash', async (done) => {
655-
Platform.OS = 'android';
656-
const report = {
657-
tags: ['tag1', 'tag2'],
658-
consoleLogs: ['consoleLog'],
659-
instabugLogs: ['instabugLog'],
660-
userAttributes: [{ age: '24' }],
661-
fileAttachments: ['path'],
662-
};
663-
NativeModules.Instabug.getReport.mockResolvedValue(report);
664-
const jsonObject = { stack: 'error' };
665-
const callback = (rep: Report) => {
666-
expect(rep).toBeInstanceOf(Report);
667-
expect(rep.tags).toBe(report.tags);
668-
expect(rep.consoleLogs).toBe(report.consoleLogs);
669-
expect(rep.instabugLogs).toBe(report.instabugLogs);
670-
expect(rep.userAttributes).toBe(report.userAttributes);
671-
expect(rep.fileAttachments).toBe(report.fileAttachments);
672-
done();
673-
};
674-
Instabug.onReportSubmitHandler(callback);
675-
IBGEventEmitter.emit(IBGConstants.SEND_HANDLED_CRASH, jsonObject);
676-
677-
expect(IBGEventEmitter.getListeners(IBGConstants.SEND_HANDLED_CRASH).length).toEqual(1);
678-
await waitForExpect(() => expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledTimes(1));
679-
await waitForExpect(() =>
680-
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledWith(jsonObject),
681-
);
682-
});
683-
684-
it('should not break if pre-sending callback fails on emitting the event IBGSendHandledJSCrash', async () => {
685-
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
686-
687-
Platform.OS = 'android';
688-
NativeModules.Instabug.getReport.mockResolvedValue({});
689-
const callback = jest.fn(() => {
690-
throw new Error('Pre-sending callback failed.');
691-
});
692-
693-
Instabug.onReportSubmitHandler(callback);
694-
IBGEventEmitter.emit(IBGConstants.SEND_HANDLED_CRASH, {});
695-
696-
// We don't care if console.error is called but we use it as a sign that the function finished running
697-
await waitForExpect(() => expect(callback).toBeCalled());
698-
expect(NativeCrashReporting.sendHandledJSCrash).not.toBeCalled();
699-
700-
consoleSpy.mockRestore();
701-
});
702-
703-
it('should not invoke callback on emitting the event IBGSendHandledJSCrash when Platform is iOS', () => {
704-
Platform.OS = 'ios';
705-
Instabug.onReportSubmitHandler(jest.fn());
706-
IBGEventEmitter.emit(IBGConstants.SEND_HANDLED_CRASH, {});
707-
expect(IBGEventEmitter.getListeners(IBGConstants.SEND_HANDLED_CRASH).length).toEqual(0);
708-
});
709-
710-
it('should invoke callback on emitting the event IBGSendUnhandledJSCrash', async (done) => {
711-
Platform.OS = 'android';
712-
const report = {
713-
tags: ['tag1', 'tag2'],
714-
consoleLogs: ['consoleLog'],
715-
instabugLogs: ['instabugLog'],
716-
userAttributes: [{ age: '24' }],
717-
fileAttachments: ['path'],
718-
};
719-
NativeModules.Instabug.getReport.mockResolvedValue(report);
720-
const jsonObject = { stack: 'error' };
721-
const callback = (rep: Report) => {
722-
expect(rep).toBeInstanceOf(Report);
723-
expect(rep.tags).toBe(report.tags);
724-
expect(rep.consoleLogs).toBe(report.consoleLogs);
725-
expect(rep.instabugLogs).toBe(report.instabugLogs);
726-
expect(rep.userAttributes).toBe(report.userAttributes);
727-
expect(rep.fileAttachments).toBe(report.fileAttachments);
728-
done();
729-
};
730-
Instabug.onReportSubmitHandler(callback);
731-
IBGEventEmitter.emit(IBGConstants.SEND_UNHANDLED_CRASH, jsonObject);
732-
733-
expect(IBGEventEmitter.getListeners(IBGConstants.SEND_UNHANDLED_CRASH).length).toEqual(1);
734-
await waitForExpect(() => expect(NativeCrashReporting.sendJSCrash).toBeCalledTimes(1));
735-
await waitForExpect(() => expect(NativeCrashReporting.sendJSCrash).toBeCalledWith(jsonObject));
736-
});
737-
738-
it('should not invoke callback on emitting the event IBGSendUnhandledJSCrash when Platform is iOS', () => {
739-
Platform.OS = 'ios';
740-
Instabug.onReportSubmitHandler(jest.fn());
741-
IBGEventEmitter.emit(IBGConstants.SEND_UNHANDLED_CRASH, {});
742-
expect(IBGEventEmitter.getListeners(IBGConstants.SEND_UNHANDLED_CRASH).length).toEqual(0);
743-
});
744-
745644
it('should invoke the native method callPrivateApi', () => {
746645
const apiName = 'name';
747646
const param = 'param';

0 commit comments

Comments
 (0)