Skip to content

Commit 1c8cb1a

Browse files
DavidMina96HeshamMegid
authored andcommitted
[INSD-8643] Add reportError and deprecate reportJSException (#852)
* Check reportJSException error type internally Enforcing a strong type for this API would require code changes from the users as the catch block parameter is of type any/unknown, so we handle the type internally. * Deprecate reportJSException and add reportError * Update CHANGELOG.md * Use reportError in tests * Use `reportError` API in example app
1 parent bcc0483 commit 1c8cb1a

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
- Deprecates CrashReporting.reportJSException in favour of a new strongly typed API: CrashReporting.reportError
34
- Fixes Survey interface export causing a build error with certain babel versions
45

56
## 11.5.0 (2022-11-28)

example/src/screens/HomeScreen.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export function HomeScreen() {
2727
try {
2828
throw new Error('Handled Exception From Instabug Test App');
2929
} catch (err) {
30-
CrashReporting.reportJSException(err);
31-
Alert.alert('Crash report Sent!');
30+
if (err instanceof Error) {
31+
CrashReporting.reportError(err);
32+
Alert.alert('Crash report Sent!');
33+
}
3234
}
3335
};
3436

src/modules/CrashReporting.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ export const setEnabled = (isEnabled: boolean) => {
1616
};
1717

1818
/**
19+
* @deprecated Use {@link reportError} instead.
1920
* Send handled JS error object
2021
* @param error Error object to be sent to Instabug's servers
2122
*/
22-
export const reportJSException = (error: ExtendedError) => {
23+
export const reportJSException = (error: any) => {
24+
if (error instanceof Error) {
25+
reportError(error);
26+
}
27+
};
28+
29+
/**
30+
* Send handled JS error object
31+
* @param error Error object to be sent to Instabug's servers
32+
*/
33+
export const reportError = (error: ExtendedError) => {
2334
const jsStackTrace = InstabugUtils.getStackTrace(error);
2435

2536
const jsonObject = {

tests/modules/CrashReporting.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('CrashReporting Module', () => {
2020
it('should call the native method sendHandledJSCrash when platform is ios', () => {
2121
Platform.OS = 'ios';
2222
const errorObject = { name: 'TypeError', message: 'Invalid type' };
23-
CrashReporting.reportJSException(errorObject);
23+
CrashReporting.reportError(errorObject);
2424

2525
const expectedObject = {
2626
message: 'TypeError - Invalid type',
@@ -38,7 +38,7 @@ describe('CrashReporting Module', () => {
3838
it('should call the native method sendHandledJSCrash when platform is android', () => {
3939
Platform.OS = 'android';
4040
const errorObject = { name: 'TypeError', message: 'Invalid type' };
41-
CrashReporting.reportJSException(errorObject);
41+
CrashReporting.reportError(errorObject);
4242

4343
const expectedObject = {
4444
message: 'TypeError - Invalid type',
@@ -75,7 +75,7 @@ describe('CrashReporting Module', () => {
7575
crashHandler,
7676
);
7777

78-
CrashReporting.reportJSException(errorObject);
78+
CrashReporting.reportError(errorObject);
7979
expect(crashHandler).toBeCalledTimes(1);
8080
expect(crashHandler).toBeCalledWith(expectedObject);
8181
});

0 commit comments

Comments
 (0)