Skip to content

Commit 0be68b3

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Refactor try/catch blocks to use unknown type annotations (#53938)
Summary: Pull Request resolved: #53938 Changelog: [internal] - Add `: unknown` annotations to all catch parameters in React Native JS files - Create toError utility for converting mixed values to Error instances - Create toExtendedError utility for LogBox-specific ExtendedError conversion - Fix Flow type compatibility issues with proper error handling - Resolve ESLint import order warnings Reviewed By: andrewdacenko Differential Revision: D82304028 fbshipit-source-id: 8b7be9a2416892fcf2f222e08d108a9ca09f3c1f
1 parent fc66884 commit 0be68b3

12 files changed

Lines changed: 87 additions & 27 deletions

File tree

packages/react-native/Libraries/Animated/animations/Animation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export default class Animation {
168168
);
169169

170170
return true;
171-
} catch (e) {
171+
} catch (e: unknown) {
172172
throw e;
173173
} finally {
174174
NativeAnimatedHelper.API.unsetWaitingForIdentifier(

packages/react-native/Libraries/BatchedBridge/MessageQueue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class MessageQueue {
372372
} else {
373373
try {
374374
fn();
375-
} catch (error) {
375+
} catch (error: unknown) {
376376
ErrorUtils.reportFatalError(error);
377377
}
378378
}

packages/react-native/Libraries/Core/Timers/JSTimers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import NativeTiming from './NativeTiming';
1313

14+
const toError = require('../../../src/private/utilities/toError').default;
1415
const BatchedBridge = require('../../BatchedBridge/BatchedBridge').default;
1516
const Systrace = require('../../Performance/Systrace');
1617
const invariant = require('invariant');
@@ -129,9 +130,9 @@ function _callTimer(timerID: number, frameTime: number, didTimeout: ?boolean) {
129130
} else {
130131
console.error('Tried to call a callback with invalid type: ' + type);
131132
}
132-
} catch (e) {
133+
} catch (e: unknown) {
133134
// Don't rethrow so that we can run all timers.
134-
errors.push(e);
135+
errors.push(toError(e));
135136
}
136137

137138
if (__DEV__) {

packages/react-native/Libraries/Core/Timers/__tests__/JSTimers-test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ const NativeTiming = {
1616
setSendIdleEvents: jest.fn(),
1717
};
1818

19-
jest
20-
.enableAutomock()
21-
.mock('../NativeTiming', () => ({
22-
__esModule: true,
23-
default: NativeTiming,
24-
}))
25-
.unmock('../JSTimers');
19+
jest.mock('../NativeTiming', () => ({
20+
__esModule: true,
21+
default: NativeTiming,
22+
}));
2623

2724
const JSTimers = require('../JSTimers').default;
2825

packages/react-native/Libraries/Core/setUpErrorHandling.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ if (global.RN$useAlwaysAvailableJSErrorHandling !== true) {
1616
* You can use this module directly, or just require InitializeCore.
1717
*/
1818
const ExceptionsManager = require('./ExceptionsManager').default;
19+
const toError = require('../../src/private/utilities/toError').default;
1920
ExceptionsManager.installConsoleErrorReporter();
2021

2122
// Set up error handler
2223
if (!global.__fbDisableExceptionsManager) {
2324
const handleError = (e: unknown, isFatal: boolean) => {
2425
try {
2526
ExceptionsManager.handleException(e, isFatal);
26-
} catch (ee) {
27-
console.log('Failed to print error: ', ee.message);
27+
} catch (ee: unknown) {
28+
const error = toError(ee);
29+
console.log('Failed to print error: ', error.message);
2830
throw e;
2931
}
3032
};

packages/react-native/Libraries/Core/setUpReactDevTools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ if (__DEV__) {
4949
try {
5050
const parsedSettings = JSON.parse(serializedHookSettings);
5151
hookSettings = parsedSettings;
52-
} catch {
52+
} catch (e: unknown) {
5353
console.error(
5454
'Failed to parse persisted React DevTools hook settings. React DevTools will be initialized with default settings.',
5555
);

packages/react-native/Libraries/Interaction/InteractionManager.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import type {EventSubscription} from '../vendor/emitter/EventEmitter';
1212

13+
const toError = require('../../src/private/utilities/toError').default;
1314
const invariant = require('invariant');
1415

1516
export type SimpleTask = {
@@ -113,8 +114,8 @@ const InteractionManagerStub = {
113114
try {
114115
task.run();
115116
resolve();
116-
} catch (error) {
117-
reject(error);
117+
} catch (error: unknown) {
118+
reject(toError(error));
118119
}
119120
} else {
120121
reject(new TypeError(`Task "${task.name}" missing gen or run.`));
@@ -123,8 +124,8 @@ const InteractionManagerStub = {
123124
try {
124125
task();
125126
resolve();
126-
} catch (error) {
127-
reject(error);
127+
} catch (error: unknown) {
128+
reject(toError(error));
128129
}
129130
} else {
130131
reject(new TypeError('Invalid task of type: ' + typeof task));

packages/react-native/Libraries/LogBox/Data/LogBoxData.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
} from './parseLogBoxLog';
2020

2121
import DebuggerSessionObserver from '../../../src/private/devsupport/rndevtools/FuseboxSessionObserver';
22+
import toExtendedError from '../../../src/private/utilities/toExtendedError';
2223
import parseErrorStack from '../../Core/Devtools/parseErrorStack';
2324
import NativeLogBox from '../../NativeModules/specs/NativeLogBox';
2425
import LogBoxLog from './LogBoxLog';
@@ -240,8 +241,8 @@ export function addLog(log: LogData): void {
240241
componentStackType: log.componentStackType || 'legacy',
241242
}),
242243
);
243-
} catch (error) {
244-
reportLogBoxError(error);
244+
} catch (error: unknown) {
245+
reportLogBoxError(toExtendedError(error));
245246
}
246247
});
247248
}
@@ -252,8 +253,8 @@ export function addException(error: ExtendedExceptionData): void {
252253
setImmediate(() => {
253254
try {
254255
appendNewLog(new LogBoxLog(parseLogBoxException(error)));
255-
} catch (loggingError) {
256-
reportLogBoxError(loggingError);
256+
} catch (loggingError: unknown) {
257+
reportLogBoxError(toExtendedError(loggingError));
257258
}
258259
});
259260
}

packages/react-native/Libraries/LogBox/LogBox.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {IgnorePattern, LogData} from './Data/LogBoxData';
1212
import type {ExtendedExceptionData} from './Data/parseLogBoxLog';
1313

14+
import toExtendedError from '../../src/private/utilities/toExtendedError';
1415
import Platform from '../Utilities/Platform';
1516
import RCTLog from '../Utilities/RCTLog';
1617
import * as React from 'react';
@@ -192,8 +193,8 @@ if (__DEV__) {
192193
componentStackType,
193194
});
194195
}
195-
} catch (err) {
196-
LogBoxData.reportLogBoxError(err);
196+
} catch (err: unknown) {
197+
LogBoxData.reportLogBoxError(toExtendedError(err));
197198
}
198199
}
199200
},
@@ -237,8 +238,8 @@ if (__DEV__) {
237238
});
238239
}
239240
}
240-
} catch (err) {
241-
LogBoxData.reportLogBoxError(err);
241+
} catch (err: unknown) {
242+
LogBoxData.reportLogBoxError(toExtendedError(err));
242243
}
243244
};
244245
} else {

packages/react-native/Libraries/Network/XMLHttpRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class XMLHttpRequest extends EventTarget {
284284
case 'json':
285285
try {
286286
this._cachedResponse = JSON.parse(this._response);
287-
} catch (_) {
287+
} catch (_: unknown) {
288288
this._cachedResponse = null;
289289
}
290290
break;

0 commit comments

Comments
 (0)