Skip to content

Commit a661619

Browse files
committed
chore: ignore non-error promise rejections
1 parent 4f49da2 commit a661619

File tree

4 files changed

+39
-57
lines changed

4 files changed

+39
-57
lines changed

src/utils/InstabugUtils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,6 @@ export const stringifyIfNotString = (input: unknown) => {
9292
return typeof input === 'string' ? input : JSON.stringify(input);
9393
};
9494

95-
export const errorifyIfNotError = (value: unknown): Error => {
96-
if (value instanceof Error) {
97-
return value;
98-
}
99-
100-
const message = stringifyIfNotString(value);
101-
const error = new Error(message);
102-
103-
// Empty the stack trace since it'd be inaccurate
104-
error.stack = '';
105-
106-
return error;
107-
};
108-
10995
export const invokeDeprecatedCallback = <T>(callback?: (arg: T) => void, arg?: T | null) => {
11096
if (!callback) {
11197
return;

src/utils/UnhandledRejectionTracking.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tracking, { RejectionTrackingOptions } from 'promise/setimmediate/rejection-tracking';
2-
import { errorifyIfNotError, sendCrashReport } from './InstabugUtils';
2+
import { sendCrashReport } from './InstabugUtils';
33
import { NativeCrashReporting } from '../native/NativeCrashReporting';
44

55
export interface HermesInternalType {
@@ -75,9 +75,9 @@ function _onUnhandled(id: number, rejection: unknown) {
7575
return;
7676
}
7777

78-
const error = errorifyIfNotError(rejection);
79-
80-
sendCrashReport(error, NativeCrashReporting.sendHandledJSCrash);
78+
if (rejection instanceof Error) {
79+
sendCrashReport(rejection, NativeCrashReporting.sendHandledJSCrash);
80+
}
8181
}
8282

8383
/* istanbul ignore next */

test/utils/InstabugUtils.spec.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import parseErrorStackLib from 'react-native/Libraries/Core/Devtools/parseErrorS
77
import * as Instabug from '../../src/modules/Instabug';
88
import { NativeCrashReporting } from '../../src/native/NativeCrashReporting';
99
import { InvocationEvent } from '../../src/utils/Enums';
10-
import InstabugUtils, {
11-
errorifyIfNotError,
12-
getStackTrace,
13-
sendCrashReport,
14-
} from '../../src/utils/InstabugUtils';
10+
import InstabugUtils, { getStackTrace, sendCrashReport } from '../../src/utils/InstabugUtils';
1511

1612
describe('Test global error handler', () => {
1713
beforeEach(() => {
@@ -265,22 +261,4 @@ describe('Instabug Utils', () => {
265261
expect(remoteSenderCallback).toHaveBeenCalledTimes(1);
266262
expect(remoteSenderCallback).toHaveBeenCalledWith(expectedMap);
267263
});
268-
269-
it('errorifyIfNotError should convert non-errors into an error', () => {
270-
const message = 'Something went wrong';
271-
272-
const result = errorifyIfNotError(message);
273-
274-
expect(result).toBeInstanceOf(Error);
275-
expect(result.message).toBe(message);
276-
expect(result.stack).toBe('');
277-
});
278-
279-
it('errorifyIfNotError should not affect errors', () => {
280-
const error = new TypeError('Something went wrong');
281-
282-
const result = errorifyIfNotError(error);
283-
284-
expect(result).toBe(error);
285-
});
286264
});

test/utils/UnhandledRejectionTracking.spec.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,25 @@ it('tracks Promise rejections when using `promise` polyfill', () => {
3636
});
3737
});
3838

39-
it.each([new Error(), "Couldn't fetch data"])(
40-
'reports unhandled Promise rejections in release mode',
41-
(rejection) => {
42-
const mockDev = mockDevMode(false);
43-
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
39+
it('reports unhandled Promise rejections in release mode', () => {
40+
const mockDev = mockDevMode(false);
41+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
4442

45-
const id = 123;
43+
const rejection = new Error('something went wrong');
44+
const id = 123;
4645

47-
// Simulate an immediate unhandled promise rejection
48-
mocked(tracking.enable).mockImplementationOnce((options) => {
49-
options?.onUnhandled?.(id, rejection);
50-
});
46+
// Simulate an immediate unhandled promise rejection
47+
mocked(tracking.enable).mockImplementationOnce((options) => {
48+
options?.onUnhandled?.(id, rejection);
49+
});
5150

52-
captureUnhandledRejections();
51+
captureUnhandledRejections();
5352

54-
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledTimes(1);
53+
expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledTimes(1);
5554

56-
mockDev.mockRestore();
57-
consoleWarnSpy.mockRestore();
58-
},
59-
);
55+
mockDev.mockRestore();
56+
consoleWarnSpy.mockRestore();
57+
});
6058

6159
it('does not report unhandled Promise rejections in dev mode', () => {
6260
const mockDev = mockDevMode(true);
@@ -77,3 +75,23 @@ it('does not report unhandled Promise rejections in dev mode', () => {
7775
mockDev.mockRestore();
7876
consoleWarnSpy.mockRestore();
7977
});
78+
79+
it('does not report non-error unhandled Promise rejections', () => {
80+
const mockDev = mockDevMode(true);
81+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
82+
83+
const id = 123;
84+
const rejection = 'something went wrong';
85+
86+
// Simulate an immediate unhandled promise rejection
87+
mocked(tracking.enable).mockImplementationOnce((options) => {
88+
options?.onUnhandled?.(id, rejection);
89+
});
90+
91+
captureUnhandledRejections();
92+
93+
expect(NativeCrashReporting.sendHandledJSCrash).not.toBeCalled();
94+
95+
mockDev.mockRestore();
96+
consoleWarnSpy.mockRestore();
97+
});

0 commit comments

Comments
 (0)